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 | |
Mike Ma | 3505666 | 2018-12-06 13:32:59 -0800 | [diff] [blame] | 21 | #include <log/log.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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; |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 50 | VLOG("Truncating data"); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 51 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 52 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 53 | if (mBuffer.writeBuffer() == NULL) { |
| 54 | VLOG("No memory"); |
| 55 | return NO_MEMORY; |
| 56 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | |
| 58 | int64_t remainingTime = (mStartTime + timeout) - uptimeMillis(); |
| 59 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 60 | VLOG("timed out due to long read"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 61 | mTimedOut = true; |
| 62 | break; |
| 63 | } |
| 64 | |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 65 | int count = TEMP_FAILURE_RETRY(poll(&pfds, 1, remainingTime)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 66 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 67 | VLOG("timed out due to block calling poll"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 68 | mTimedOut = true; |
| 69 | break; |
| 70 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 71 | VLOG("poll failed: %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 72 | return -errno; |
| 73 | } else { |
| 74 | if ((pfds.revents & POLLERR) != 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 75 | VLOG("return event has error %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 76 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 77 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 78 | ssize_t amt = TEMP_FAILURE_RETRY( |
| 79 | ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 | if (amt < 0) { |
| 81 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 82 | continue; |
| 83 | } else { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 84 | VLOG("Fail to read %d: %s", fd, strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | return -errno; |
| 86 | } |
| 87 | } else if (amt == 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 88 | VLOG("Reached EOF of fd=%d", fd); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 89 | break; |
| 90 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 91 | mBuffer.wp()->move(amt); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 92 | } |
| 93 | } |
| 94 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 95 | mFinishTime = uptimeMillis(); |
| 96 | return NO_ERROR; |
| 97 | } |
| 98 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 99 | status_t FdBuffer::readFully(int fd) { |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 100 | mStartTime = uptimeMillis(); |
| 101 | |
| 102 | while (true) { |
| 103 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 104 | // Don't let it get too big. |
| 105 | mTruncated = true; |
| 106 | VLOG("Truncating data"); |
| 107 | break; |
| 108 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 109 | if (mBuffer.writeBuffer() == NULL) { |
| 110 | VLOG("No memory"); |
| 111 | return NO_MEMORY; |
| 112 | } |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 113 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 114 | ssize_t amt = |
| 115 | TEMP_FAILURE_RETRY(::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 116 | if (amt < 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 117 | VLOG("Fail to read %d: %s", fd, strerror(errno)); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 118 | return -errno; |
| 119 | } else if (amt == 0) { |
| 120 | VLOG("Done reading %zu bytes", mBuffer.size()); |
| 121 | // We're done. |
| 122 | break; |
| 123 | } |
| 124 | mBuffer.wp()->move(amt); |
| 125 | } |
| 126 | |
| 127 | mFinishTime = uptimeMillis(); |
| 128 | return NO_ERROR; |
| 129 | } |
| 130 | |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 131 | status_t FdBuffer::readProcessedDataInStream(int fd, unique_fd toFd, unique_fd fromFd, |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 132 | int64_t timeoutMs, const bool isSysfs) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 133 | struct pollfd pfds[] = { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 134 | {.fd = fd, .events = POLLIN}, |
| 135 | {.fd = toFd.get(), .events = POLLOUT}, |
| 136 | {.fd = fromFd.get(), .events = POLLIN}, |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 137 | }; |
| 138 | |
| 139 | mStartTime = uptimeMillis(); |
| 140 | |
| 141 | // mark all fds non blocking |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 142 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 143 | fcntl(toFd.get(), F_SETFL, fcntl(toFd.get(), F_GETFL, 0) | O_NONBLOCK); |
| 144 | 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] | 145 | |
| 146 | // A circular buffer holds data read from fd and writes to parsing process |
| 147 | uint8_t cirBuf[BUFFER_SIZE]; |
| 148 | size_t cirSize = 0; |
| 149 | int rpos = 0, wpos = 0; |
| 150 | |
| 151 | // This is the buffer used to store processed data |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 152 | while (true) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 153 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 154 | VLOG("Truncating data"); |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 155 | mTruncated = true; |
| 156 | break; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 157 | } |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 158 | if (mBuffer.writeBuffer() == NULL) { |
| 159 | VLOG("No memory"); |
| 160 | return NO_MEMORY; |
| 161 | } |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 162 | |
| 163 | int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis(); |
| 164 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 165 | VLOG("timed out due to long read"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 166 | mTimedOut = true; |
| 167 | break; |
| 168 | } |
| 169 | |
| 170 | // wait for any pfds to be ready to perform IO |
Mike Ma | 2838169 | 2018-12-04 15:46:29 -0800 | [diff] [blame] | 171 | int count = TEMP_FAILURE_RETRY(poll(pfds, 3, remainingTime)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 172 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 173 | VLOG("timed out due to block calling poll"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 174 | mTimedOut = true; |
| 175 | break; |
| 176 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 177 | VLOG("Fail to poll: %s", strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 178 | return -errno; |
| 179 | } |
| 180 | |
| 181 | // make sure no errors occur on any fds |
| 182 | for (int i = 0; i < 3; ++i) { |
| 183 | if ((pfds[i].revents & POLLERR) != 0) { |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 184 | if (i == 0 && isSysfs) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 185 | VLOG("fd %d is sysfs, ignore its POLLERR return value", fd); |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 186 | continue; |
| 187 | } |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 188 | VLOG("fd[%d]=%d returns error events: %s", i, fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 189 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 190 | } |
| 191 | } |
| 192 | |
| 193 | // read from fd |
| 194 | if (cirSize != BUFFER_SIZE && pfds[0].fd != -1) { |
| 195 | ssize_t amt; |
| 196 | if (rpos >= wpos) { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 197 | amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, BUFFER_SIZE - rpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 198 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 199 | amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, wpos - rpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 200 | } |
| 201 | if (amt < 0) { |
| 202 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 203 | VLOG("Fail to read fd %d: %s", fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 204 | return -errno; |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 205 | } // otherwise just continue |
| 206 | } else if (amt == 0) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 207 | VLOG("Reached EOF of input file %d", fd); |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 208 | 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] | 209 | } else { |
| 210 | rpos += amt; |
| 211 | cirSize += amt; |
| 212 | } |
| 213 | } |
| 214 | |
| 215 | // write to parsing process |
| 216 | if (cirSize > 0 && pfds[1].fd != -1) { |
| 217 | ssize_t amt; |
| 218 | if (rpos > wpos) { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 219 | amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, rpos - wpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 220 | } else { |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 221 | amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, BUFFER_SIZE - wpos)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 222 | } |
| 223 | if (amt < 0) { |
| 224 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 225 | VLOG("Fail to write toFd %d: %s", toFd.get(), strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 226 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 227 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 228 | } else { |
| 229 | wpos += amt; |
| 230 | cirSize -= amt; |
| 231 | } |
| 232 | } |
| 233 | |
| 234 | // if buffer is empty and fd is closed, close write fd. |
| 235 | if (cirSize == 0 && pfds[0].fd == -1 && pfds[1].fd != -1) { |
Yi Jin | e3dab2d | 2018-03-22 16:56:39 -0700 | [diff] [blame] | 236 | VLOG("Close write pipe %d", toFd.get()); |
| 237 | toFd.reset(); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 238 | pfds[1].fd = -1; |
| 239 | } |
| 240 | |
| 241 | // circular buffer, reset rpos and wpos |
| 242 | if (rpos >= BUFFER_SIZE) { |
| 243 | rpos = 0; |
| 244 | } |
| 245 | if (wpos >= BUFFER_SIZE) { |
| 246 | wpos = 0; |
| 247 | } |
| 248 | |
| 249 | // read from parsing process |
Yi Jin | 0e5d577 | 2018-04-20 11:16:10 -0700 | [diff] [blame] | 250 | ssize_t amt = TEMP_FAILURE_RETRY( |
| 251 | ::read(fromFd.get(), mBuffer.writeBuffer(), mBuffer.currentToWrite())); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 252 | if (amt < 0) { |
| 253 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | ea31cbe | 2018-04-02 11:09:57 -0700 | [diff] [blame] | 254 | VLOG("Fail to read fromFd %d: %s", fromFd.get(), strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 255 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 256 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 257 | } else if (amt == 0) { |
Yi Jin | ea31cbe | 2018-04-02 11:09:57 -0700 | [diff] [blame] | 258 | VLOG("Reached EOF of fromFd %d", fromFd.get()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 259 | break; |
| 260 | } else { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 261 | mBuffer.wp()->move(amt); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 262 | } |
| 263 | } |
| 264 | |
| 265 | mFinishTime = uptimeMillis(); |
| 266 | return NO_ERROR; |
| 267 | } |
| 268 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 269 | size_t FdBuffer::size() const { return mBuffer.size(); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 270 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 271 | EncodedBuffer::iterator FdBuffer::data() const { return mBuffer.begin(); } |
Yi Jin | 6cacbcb | 2018-03-30 14:04:52 -0700 | [diff] [blame] | 272 | |
| 273 | } // namespace incidentd |
| 274 | } // namespace os |
| 275 | } // namespace android |