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 | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 16 | #include "Log.h" |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 17 | |
| 18 | #include "FdBuffer.h" |
| 19 | |
| 20 | #include <cutils/log.h> |
| 21 | #include <utils/SystemClock.h> |
| 22 | |
| 23 | #include <fcntl.h> |
| 24 | #include <poll.h> |
| 25 | #include <unistd.h> |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 26 | #include <wait.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 27 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 28 | const ssize_t BUFFER_SIZE = 16 * 1024; // 16 KB |
| 29 | const ssize_t MAX_BUFFER_COUNT = 256; // 4 MB max |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 30 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | FdBuffer::FdBuffer() |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 32 | : mBuffer(BUFFER_SIZE), mStartTime(-1), mFinishTime(-1), mTimedOut(false), mTruncated(false) {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 33 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 34 | FdBuffer::~FdBuffer() {} |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 35 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 36 | status_t FdBuffer::read(int fd, int64_t timeout) { |
| 37 | struct pollfd pfds = {.fd = fd, .events = POLLIN}; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 38 | mStartTime = uptimeMillis(); |
| 39 | |
| 40 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 41 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | while (true) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 43 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 44 | mTruncated = true; |
| 45 | break; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 46 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 47 | if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 48 | |
| 49 | int64_t remainingTime = (mStartTime + timeout) - uptimeMillis(); |
| 50 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 51 | VLOG("timed out due to long read"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 52 | mTimedOut = true; |
| 53 | break; |
| 54 | } |
| 55 | |
| 56 | int count = poll(&pfds, 1, remainingTime); |
| 57 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 58 | VLOG("timed out due to block calling poll"); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 59 | mTimedOut = true; |
| 60 | break; |
| 61 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 62 | VLOG("poll failed: %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 63 | return -errno; |
| 64 | } else { |
| 65 | if ((pfds.revents & POLLERR) != 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 66 | VLOG("return event has error %s", strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 67 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 68 | } else { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 69 | ssize_t amt = ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite()); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 70 | if (amt < 0) { |
| 71 | if (errno == EAGAIN || errno == EWOULDBLOCK) { |
| 72 | continue; |
| 73 | } else { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 74 | VLOG("Fail to read %d: %s", fd, strerror(errno)); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 75 | return -errno; |
| 76 | } |
| 77 | } else if (amt == 0) { |
| 78 | break; |
| 79 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 80 | mBuffer.wp()->move(amt); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 81 | } |
| 82 | } |
| 83 | } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 84 | mFinishTime = uptimeMillis(); |
| 85 | return NO_ERROR; |
| 86 | } |
| 87 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 88 | status_t FdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs, |
| 89 | const bool isSysfs) { |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 90 | struct pollfd pfds[] = { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 91 | {.fd = fd, .events = POLLIN}, |
| 92 | {.fd = toFd, .events = POLLOUT}, |
| 93 | {.fd = fromFd, .events = POLLIN}, |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 94 | }; |
| 95 | |
| 96 | mStartTime = uptimeMillis(); |
| 97 | |
| 98 | // mark all fds non blocking |
| 99 | fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); |
| 100 | fcntl(toFd, F_SETFL, fcntl(toFd, F_GETFL, 0) | O_NONBLOCK); |
| 101 | fcntl(fromFd, F_SETFL, fcntl(fromFd, F_GETFL, 0) | O_NONBLOCK); |
| 102 | |
| 103 | // A circular buffer holds data read from fd and writes to parsing process |
| 104 | uint8_t cirBuf[BUFFER_SIZE]; |
| 105 | size_t cirSize = 0; |
| 106 | int rpos = 0, wpos = 0; |
| 107 | |
| 108 | // This is the buffer used to store processed data |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 109 | while (true) { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 110 | if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { |
| 111 | mTruncated = true; |
| 112 | break; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 113 | } |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 114 | if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 115 | |
| 116 | int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis(); |
| 117 | if (remainingTime <= 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 118 | VLOG("timed out due to long read"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 119 | mTimedOut = true; |
| 120 | break; |
| 121 | } |
| 122 | |
| 123 | // wait for any pfds to be ready to perform IO |
| 124 | int count = poll(pfds, 3, remainingTime); |
| 125 | if (count == 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 126 | VLOG("timed out due to block calling poll"); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 127 | mTimedOut = true; |
| 128 | break; |
| 129 | } else if (count < 0) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 130 | VLOG("Fail to poll: %s", strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 131 | return -errno; |
| 132 | } |
| 133 | |
| 134 | // make sure no errors occur on any fds |
| 135 | for (int i = 0; i < 3; ++i) { |
| 136 | if ((pfds[i].revents & POLLERR) != 0) { |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 137 | if (i == 0 && isSysfs) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 138 | VLOG("fd %d is sysfs, ignore its POLLERR return value", fd); |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 139 | continue; |
| 140 | } |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 141 | VLOG("fd[%d]=%d returns error events: %s", i, fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 142 | return errno != 0 ? -errno : UNKNOWN_ERROR; |
| 143 | } |
| 144 | } |
| 145 | |
| 146 | // read from fd |
| 147 | if (cirSize != BUFFER_SIZE && pfds[0].fd != -1) { |
| 148 | ssize_t amt; |
| 149 | if (rpos >= wpos) { |
| 150 | amt = ::read(fd, cirBuf + rpos, BUFFER_SIZE - rpos); |
| 151 | } else { |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 152 | amt = ::read(fd, cirBuf + rpos, wpos - rpos); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 153 | } |
| 154 | if (amt < 0) { |
| 155 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 156 | VLOG("Fail to read fd %d: %s", fd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 157 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 158 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 159 | } else if (amt == 0) { // reach EOF so don't have to poll pfds[0]. |
| 160 | ::close(pfds[0].fd); |
| 161 | pfds[0].fd = -1; |
| 162 | } else { |
| 163 | rpos += amt; |
| 164 | cirSize += amt; |
| 165 | } |
| 166 | } |
| 167 | |
| 168 | // write to parsing process |
| 169 | if (cirSize > 0 && pfds[1].fd != -1) { |
| 170 | ssize_t amt; |
| 171 | if (rpos > wpos) { |
| 172 | amt = ::write(toFd, cirBuf + wpos, rpos - wpos); |
| 173 | } else { |
| 174 | amt = ::write(toFd, cirBuf + wpos, BUFFER_SIZE - wpos); |
| 175 | } |
| 176 | if (amt < 0) { |
| 177 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 178 | VLOG("Fail to write toFd %d: %s", toFd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 179 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 180 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 181 | } else { |
| 182 | wpos += amt; |
| 183 | cirSize -= amt; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | // if buffer is empty and fd is closed, close write fd. |
| 188 | if (cirSize == 0 && pfds[0].fd == -1 && pfds[1].fd != -1) { |
| 189 | ::close(pfds[1].fd); |
| 190 | pfds[1].fd = -1; |
| 191 | } |
| 192 | |
| 193 | // circular buffer, reset rpos and wpos |
| 194 | if (rpos >= BUFFER_SIZE) { |
| 195 | rpos = 0; |
| 196 | } |
| 197 | if (wpos >= BUFFER_SIZE) { |
| 198 | wpos = 0; |
| 199 | } |
| 200 | |
| 201 | // read from parsing process |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 202 | ssize_t amt = ::read(fromFd, mBuffer.writeBuffer(), mBuffer.currentToWrite()); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 203 | if (amt < 0) { |
| 204 | if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 205 | VLOG("Fail to read fromFd %d: %s", fromFd, strerror(errno)); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 206 | return -errno; |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 207 | } // otherwise just continue |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 208 | } else if (amt == 0) { |
| 209 | break; |
| 210 | } else { |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 211 | mBuffer.wp()->move(amt); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 212 | } |
| 213 | } |
| 214 | |
| 215 | mFinishTime = uptimeMillis(); |
| 216 | return NO_ERROR; |
| 217 | } |
| 218 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 219 | size_t FdBuffer::size() const { return mBuffer.size(); } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 220 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame^] | 221 | EncodedBuffer::iterator FdBuffer::data() const { return mBuffer.begin(); } |