blob: a8ef8311720d3cb5b4e214d279dce75d3bd65d90 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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 Jin4e843102018-02-14 15:36:18 -080016#define DEBUG false
Yi Jinb592e3b2018-02-01 15:17:04 -080017#include "Log.h"
Joe Onorato1754d742016-11-21 17:51:35 -080018
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 Jin0a3406f2017-06-22 19:23:11 -070027#include <wait.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028
Yi Jin6cacbcb2018-03-30 14:04:52 -070029namespace android {
30namespace os {
31namespace incidentd {
32
Yi Jinb592e3b2018-02-01 15:17:04 -080033const ssize_t BUFFER_SIZE = 16 * 1024; // 16 KB
34const ssize_t MAX_BUFFER_COUNT = 256; // 4 MB max
Joe Onorato1754d742016-11-21 17:51:35 -080035
Joe Onorato1754d742016-11-21 17:51:35 -080036FdBuffer::FdBuffer()
Yi Jinb592e3b2018-02-01 15:17:04 -080037 : mBuffer(BUFFER_SIZE), mStartTime(-1), mFinishTime(-1), mTimedOut(false), mTruncated(false) {}
Joe Onorato1754d742016-11-21 17:51:35 -080038
Yi Jinb592e3b2018-02-01 15:17:04 -080039FdBuffer::~FdBuffer() {}
Joe Onorato1754d742016-11-21 17:51:35 -080040
Yi Jine3dab2d2018-03-22 16:56:39 -070041status_t FdBuffer::read(int fd, int64_t timeout) {
42 struct pollfd pfds = {.fd = fd, .events = POLLIN};
Joe Onorato1754d742016-11-21 17:51:35 -080043 mStartTime = uptimeMillis();
44
Yi Jine3dab2d2018-03-22 16:56:39 -070045 fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK);
Joe Onorato1754d742016-11-21 17:51:35 -080046
Joe Onorato1754d742016-11-21 17:51:35 -080047 while (true) {
Yi Jinc23fad22017-09-15 17:24:59 -070048 if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) {
49 mTruncated = true;
50 break;
Joe Onorato1754d742016-11-21 17:51:35 -080051 }
Yi Jinc23fad22017-09-15 17:24:59 -070052 if (mBuffer.writeBuffer() == NULL) return NO_MEMORY;
Joe Onorato1754d742016-11-21 17:51:35 -080053
54 int64_t remainingTime = (mStartTime + timeout) - uptimeMillis();
55 if (remainingTime <= 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080056 VLOG("timed out due to long read");
Joe Onorato1754d742016-11-21 17:51:35 -080057 mTimedOut = true;
58 break;
59 }
60
61 int count = poll(&pfds, 1, remainingTime);
62 if (count == 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080063 VLOG("timed out due to block calling poll");
Joe Onorato1754d742016-11-21 17:51:35 -080064 mTimedOut = true;
65 break;
66 } else if (count < 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080067 VLOG("poll failed: %s", strerror(errno));
Joe Onorato1754d742016-11-21 17:51:35 -080068 return -errno;
69 } else {
70 if ((pfds.revents & POLLERR) != 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -080071 VLOG("return event has error %s", strerror(errno));
Joe Onorato1754d742016-11-21 17:51:35 -080072 return errno != 0 ? -errno : UNKNOWN_ERROR;
73 } else {
Yi Jin0e5d5772018-04-20 11:16:10 -070074 ssize_t amt = TEMP_FAILURE_RETRY(
75 ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite()));
Joe Onorato1754d742016-11-21 17:51:35 -080076 if (amt < 0) {
77 if (errno == EAGAIN || errno == EWOULDBLOCK) {
78 continue;
79 } else {
Yi Jine3dab2d2018-03-22 16:56:39 -070080 VLOG("Fail to read %d: %s", fd, strerror(errno));
Joe Onorato1754d742016-11-21 17:51:35 -080081 return -errno;
82 }
83 } else if (amt == 0) {
Yi Jine3dab2d2018-03-22 16:56:39 -070084 VLOG("Reached EOF of fd=%d", fd);
Joe Onorato1754d742016-11-21 17:51:35 -080085 break;
86 }
Yi Jinc23fad22017-09-15 17:24:59 -070087 mBuffer.wp()->move(amt);
Joe Onorato1754d742016-11-21 17:51:35 -080088 }
89 }
90 }
Joe Onorato1754d742016-11-21 17:51:35 -080091 mFinishTime = uptimeMillis();
92 return NO_ERROR;
93}
94
Yi Jine3dab2d2018-03-22 16:56:39 -070095status_t FdBuffer::readFully(int fd) {
Kweku Adamseadd1232018-02-05 16:45:13 -080096 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 Jine3dab2d2018-03-22 16:56:39 -0700107 ssize_t amt =
108 TEMP_FAILURE_RETRY(::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite()));
Kweku Adamseadd1232018-02-05 16:45:13 -0800109 if (amt < 0) {
Yi Jine3dab2d2018-03-22 16:56:39 -0700110 VLOG("Fail to read %d: %s", fd, strerror(errno));
Kweku Adamseadd1232018-02-05 16:45:13 -0800111 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 Jine3dab2d2018-03-22 16:56:39 -0700124status_t FdBuffer::readProcessedDataInStream(int fd, unique_fd toFd, unique_fd fromFd,
Yi Jin6355d2f2018-03-14 15:18:02 -0700125 int64_t timeoutMs, const bool isSysfs) {
Yi Jin0a3406f2017-06-22 19:23:11 -0700126 struct pollfd pfds[] = {
Yi Jine3dab2d2018-03-22 16:56:39 -0700127 {.fd = fd, .events = POLLIN},
128 {.fd = toFd.get(), .events = POLLOUT},
129 {.fd = fromFd.get(), .events = POLLIN},
Yi Jin0a3406f2017-06-22 19:23:11 -0700130 };
131
132 mStartTime = uptimeMillis();
133
134 // mark all fds non blocking
Yi Jine3dab2d2018-03-22 16:56:39 -0700135 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 Jin0a3406f2017-06-22 19:23:11 -0700138
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 Jin0a3406f2017-06-22 19:23:11 -0700145 while (true) {
Yi Jinc23fad22017-09-15 17:24:59 -0700146 if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) {
147 mTruncated = true;
148 break;
Yi Jin0a3406f2017-06-22 19:23:11 -0700149 }
Yi Jinc23fad22017-09-15 17:24:59 -0700150 if (mBuffer.writeBuffer() == NULL) return NO_MEMORY;
Yi Jin0a3406f2017-06-22 19:23:11 -0700151
152 int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis();
153 if (remainingTime <= 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800154 VLOG("timed out due to long read");
Yi Jin0a3406f2017-06-22 19:23:11 -0700155 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 Jinb592e3b2018-02-01 15:17:04 -0800162 VLOG("timed out due to block calling poll");
Yi Jin0a3406f2017-06-22 19:23:11 -0700163 mTimedOut = true;
164 break;
165 } else if (count < 0) {
Yi Jinb592e3b2018-02-01 15:17:04 -0800166 VLOG("Fail to poll: %s", strerror(errno));
Yi Jin0a3406f2017-06-22 19:23:11 -0700167 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 Jin0eb22342017-11-06 17:17:27 -0800173 if (i == 0 && isSysfs) {
Yi Jine3dab2d2018-03-22 16:56:39 -0700174 VLOG("fd %d is sysfs, ignore its POLLERR return value", fd);
Yi Jin0eb22342017-11-06 17:17:27 -0800175 continue;
176 }
Yi Jine3dab2d2018-03-22 16:56:39 -0700177 VLOG("fd[%d]=%d returns error events: %s", i, fd, strerror(errno));
Yi Jin0a3406f2017-06-22 19:23:11 -0700178 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 Jin0e5d5772018-04-20 11:16:10 -0700186 amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, BUFFER_SIZE - rpos));
Yi Jin0a3406f2017-06-22 19:23:11 -0700187 } else {
Yi Jin0e5d5772018-04-20 11:16:10 -0700188 amt = TEMP_FAILURE_RETRY(::read(fd, cirBuf + rpos, wpos - rpos));
Yi Jin0a3406f2017-06-22 19:23:11 -0700189 }
190 if (amt < 0) {
191 if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
Yi Jine3dab2d2018-03-22 16:56:39 -0700192 VLOG("Fail to read fd %d: %s", fd, strerror(errno));
Yi Jin0a3406f2017-06-22 19:23:11 -0700193 return -errno;
Yi Jin1a11fa12018-02-22 16:44:10 -0800194 } // otherwise just continue
195 } else if (amt == 0) {
Yi Jine3dab2d2018-03-22 16:56:39 -0700196 VLOG("Reached EOF of input file %d", fd);
Yi Jin1a11fa12018-02-22 16:44:10 -0800197 pfds[0].fd = -1; // reach EOF so don't have to poll pfds[0].
Yi Jin0a3406f2017-06-22 19:23:11 -0700198 } 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 Jin0e5d5772018-04-20 11:16:10 -0700208 amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, rpos - wpos));
Yi Jin0a3406f2017-06-22 19:23:11 -0700209 } else {
Yi Jin0e5d5772018-04-20 11:16:10 -0700210 amt = TEMP_FAILURE_RETRY(::write(toFd.get(), cirBuf + wpos, BUFFER_SIZE - wpos));
Yi Jin0a3406f2017-06-22 19:23:11 -0700211 }
212 if (amt < 0) {
213 if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
Yi Jin6cacbcb2018-03-30 14:04:52 -0700214 VLOG("Fail to write toFd %d: %s", toFd.get(), strerror(errno));
Yi Jin0a3406f2017-06-22 19:23:11 -0700215 return -errno;
Yi Jinb592e3b2018-02-01 15:17:04 -0800216 } // otherwise just continue
Yi Jin0a3406f2017-06-22 19:23:11 -0700217 } 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 Jine3dab2d2018-03-22 16:56:39 -0700225 VLOG("Close write pipe %d", toFd.get());
226 toFd.reset();
Yi Jin0a3406f2017-06-22 19:23:11 -0700227 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 Jin0e5d5772018-04-20 11:16:10 -0700239 ssize_t amt = TEMP_FAILURE_RETRY(
240 ::read(fromFd.get(), mBuffer.writeBuffer(), mBuffer.currentToWrite()));
Yi Jin0a3406f2017-06-22 19:23:11 -0700241 if (amt < 0) {
242 if (!(errno == EAGAIN || errno == EWOULDBLOCK)) {
Yi Jinea31cbe2018-04-02 11:09:57 -0700243 VLOG("Fail to read fromFd %d: %s", fromFd.get(), strerror(errno));
Yi Jin0a3406f2017-06-22 19:23:11 -0700244 return -errno;
Yi Jinb592e3b2018-02-01 15:17:04 -0800245 } // otherwise just continue
Yi Jin0a3406f2017-06-22 19:23:11 -0700246 } else if (amt == 0) {
Yi Jinea31cbe2018-04-02 11:09:57 -0700247 VLOG("Reached EOF of fromFd %d", fromFd.get());
Yi Jin0a3406f2017-06-22 19:23:11 -0700248 break;
249 } else {
Yi Jinc23fad22017-09-15 17:24:59 -0700250 mBuffer.wp()->move(amt);
Yi Jin0a3406f2017-06-22 19:23:11 -0700251 }
252 }
253
254 mFinishTime = uptimeMillis();
255 return NO_ERROR;
256}
257
Yi Jinb592e3b2018-02-01 15:17:04 -0800258size_t FdBuffer::size() const { return mBuffer.size(); }
Joe Onorato1754d742016-11-21 17:51:35 -0800259
Yi Jinb592e3b2018-02-01 15:17:04 -0800260EncodedBuffer::iterator FdBuffer::data() const { return mBuffer.begin(); }
Yi Jin6cacbcb2018-03-30 14:04:52 -0700261
262} // namespace incidentd
263} // namespace os
264} // namespace android