Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2016 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
Yi Jin | 4e84310 | 2018-02-14 15:36:18 -0800 | [diff] [blame] | 16 | #define DEBUG false |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 17 | #include "Log.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 18 | |
| 19 | #include "FdBuffer.h" |
| 20 | |
| 21 | #include <cutils/log.h> |
| 22 | #include <utils/SystemClock.h> |
| 23 | |
| 24 | #include <fcntl.h> |
| 25 | #include <poll.h> |
| 26 | #include <unistd.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 27 | #include <wait.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 29 | namespace android { |
| 30 | namespace os { |
| 31 | namespace incidentd { |
| 32 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 33 | const ssize_t BUFFER_SIZE = 16 * 1024; // 16 KB |
| 34 | const ssize_t MAX_BUFFER_COUNT = 256; // 4 MB max |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 35 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 36 | FdBuffer::FdBuffer() |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 37 | : mBuffer(BUFFER_SIZE), mStartTime(-1), mFinishTime(-1), mTimedOut(false), mTruncated(false) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 38 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 39 | FdBuffer::~FdBuffer() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 40 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 41 | status_t FdBuffer::read(int fd, int64_t timeout) { |
| 42 | struct pollfd pfds = {.fd = fd, .events = POLLIN}; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 43 | mStartTime = uptimeMillis(); |
| 44 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 45 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 46 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | while (true) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 48 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 49 | mTruncated = true; |
| 50 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 51 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 52 | if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 53 | |
| 54 | int64_t remainingTime = (mStartTime + timeout) - uptimeMillis(); |
| 55 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 56 | VLOG("timed out due to long read"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | mTimedOut = true; |
| 58 | break; |
| 59 | } |
| 60 | |
| 61 | int count = poll(&pfds, 1, remainingTime); |
| 62 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 63 | VLOG("timed out due to block calling poll"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 64 | mTimedOut = true; |
| 65 | break; |
| 66 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 67 | VLOG("poll failed: %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 68 | return -errno; |
| 69 | } else { |
| 70 | if ((pfds.revents & POLLERR) != 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 71 | VLOG("return event has error %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 72 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 73 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 74 | ssize_t amt = TEMP_FAILURE_RETRY( |
| 75 | ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | if (amt < 0) { |
| 77 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 78 | continue; |
| 79 | } else { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 80 | VLOG("Fail to read %d: %s", fd, strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 81 | return -errno; |
| 82 | } |
| 83 | } else if (amt == 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 84 | VLOG("Reached EOF of fd=%d", fd); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | break; |
| 86 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 87 | mBuffer.wp()->move(amt); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 91 | mFinishTime = uptimeMillis(); |
| 92 | return NO_ERROR; |
| 93 | } |
| 94 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 95 | status_t FdBuffer::readFully(int fd) { |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 96 | mStartTime = uptimeMillis(); |
| 97 | |
| 98 | while (true) { |
| 99 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 100 | // Don't let it get too big. |
| 101 | mTruncated = true; |
| 102 | VLOG("Truncating data"); |
| 103 | break; |
| 104 | } |
| 105 | if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; |
| 106 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 107 | ssize_t amt = |
| 108 | TEMP_FAILURE_RETRY(::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 109 | if (amt < 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 110 | VLOG("Fail to read %d: %s", fd, strerror(errno)); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 111 | return -errno; |
| 112 | } else if (amt == 0) { |
| 113 | VLOG("Done reading %zu bytes", mBuffer.size()); |
| 114 | // We're done. |
| 115 | break; |
| 116 | } |
| 117 | mBuffer.wp()->move(amt); |
| 118 | } |
| 119 | |
| 120 | mFinishTime = uptimeMillis(); |
| 121 | return NO_ERROR; |
| 122 | } |
| 123 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 124 | status_t FdBuffer::readProcessedDataInStream(int fd, unique_fd toFd, unique_fd fromFd, |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 125 | int64_t timeoutMs, const bool isSysfs) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 126 | struct pollfd pfds[] = { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 127 | {.fd = fd, .events = POLLIN}, |
| 128 | {.fd = toFd.get(), .events = POLLOUT}, |
| 129 | {.fd = fromFd.get(), .events = POLLIN}, |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 130 | }; |
| 131 | |
| 132 | mStartTime = uptimeMillis(); |
| 133 | |
| 134 | // mark all fds non blocking |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 135 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 136 | fcntl(toFd.get(), F_SETFL, fcntl(toFd.get(), F_GETFL, 0) | O_NONBLOCK); |
| 137 | fcntl(fromFd.get(), F_SETFL, fcntl(fromFd.get(), F_GETFL, 0) | O_NONBLOCK); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 138 | |
| 139 | // A circular buffer holds data read from fd and writes to parsing process |
| 140 | uint8_t cirBuf[BUFFER_SIZE]; |
| 141 | size_t cirSize = 0; |
| 142 | int rpos = 0, wpos = 0; |
| 143 | |
| 144 | // This is the buffer used to store processed data |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 145 | while (true) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 146 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 147 | mTruncated = true; |
| 148 | break; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 149 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 150 | if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 151 | |
| 152 | int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis(); |
| 153 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 154 | VLOG("timed out due to long read"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 155 | mTimedOut = true; |
| 156 | break; |
| 157 | } |
| 158 | |
| 159 | // wait for any pfds to be ready to perform IO |
| 160 | int count = poll(pfds, 3, remainingTime); |
| 161 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 162 | VLOG("timed out due to block calling poll"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 163 | mTimedOut = true; |
| 164 | break; |
| 165 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 166 | VLOG("Fail to poll: %s", strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 167 | return -errno; |
| 168 | } |
| 169 | |
| 170 | // make sure no errors occur on any fds |
| 171 | for (int i = 0; i < 3; ++i) { |
| 172 | if ((pfds[i].revents & POLLERR) != 0) { |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 173 | if (i == 0 && isSysfs) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 174 | VLOG("fd %d is sysfs, ignore its POLLERR return value", fd); |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 175 | continue; |
| 176 | } |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 177 | VLOG("fd[%d]=%d returns error events: %s", i, fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 178 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | // read from fd |
| 183 | if (cirSize != BUFFER_SIZE && pfds[0].fd != -1) { |
| 184 | ssize_t amt; |
| 185 | if (rpos >= wpos) { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 186 | amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, BUFFER_SIZE - rpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 187 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 188 | amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, wpos - rpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 189 | } |
| 190 | if (amt < 0) { |
| 191 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 192 | VLOG("Fail to read fd %d: %s", fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 193 | return -errno; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 194 | } // otherwise just continue |
| 195 | } else if (amt == 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 196 | VLOG("Reached EOF of input file %d", fd); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 197 | pfds[0].fd = -1; // reach EOF so don't have to poll pfds[0]. |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 198 | } else { |
| 199 | rpos += amt; |
| 200 | cirSize += amt; |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | // write to parsing process |
| 205 | if (cirSize > 0 && pfds[1].fd != -1) { |
| 206 | ssize_t amt; |
| 207 | if (rpos > wpos) { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 208 | amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, rpos - wpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 209 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 210 | amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, BUFFER_SIZE - wpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 211 | } |
| 212 | if (amt < 0) { |
| 213 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 214 | VLOG("Fail to write toFd %d: %s", toFd.get(), strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 215 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 216 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 217 | } else { |
| 218 | wpos += amt; |
| 219 | cirSize -= amt; |
| 220 | } |
| 221 | } |
| 222 | |
| 223 | // if buffer is empty and fd is closed, close write fd. |
| 224 | if (cirSize == 0 && pfds[0].fd == -1 && pfds[1].fd != -1) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 225 | VLOG("Close write pipe %d", toFd.get()); |
| 226 | toFd.reset(); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 227 | pfds[1].fd = -1; |
| 228 | } |
| 229 | |
| 230 | // circular buffer, reset rpos and wpos |
| 231 | if (rpos >= BUFFER_SIZE) { |
| 232 | rpos = 0; |
| 233 | } |
| 234 | if (wpos >= BUFFER_SIZE) { |
| 235 | wpos = 0; |
| 236 | } |
| 237 | |
| 238 | // read from parsing process |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 239 | ssize_t amt = TEMP_FAILURE_RETRY( |
| 240 | ::read(fromFd.get(), mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 241 | if (amt < 0) { |
| 242 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | ea31cbe | 2018-04-02 11:09:57 -0700 | [diff] [blame] | 243 | VLOG("Fail to read fromFd %d: %s", fromFd.get(), strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 244 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 245 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 246 | } else if (amt == 0) { |
Yi Jin | ea31cbe | 2018-04-02 11:09:57 -0700 | [diff] [blame] | 247 | VLOG("Reached EOF of fromFd %d", fromFd.get()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 248 | break; |
| 249 | } else { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 250 | mBuffer.wp()->move(amt); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
| 254 | mFinishTime = uptimeMillis(); |
| 255 | return NO_ERROR; |
| 256 | } |
| 257 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 258 | size_t FdBuffer::size() const { return mBuffer.size(); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 259 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 260 | EncodedBuffer::iterator FdBuffer::data() const { return mBuffer.begin(); } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 261 | |
| 262 | } // namespace incidentd |
| 263 | } // namespace os |
| 264 | } // namespace android |