FdBuffer.cpp revision 0eb223496c3dee6bd0a33ea4fa664fb92ef557cc
11754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato/* 21754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * Copyright (C) 2016 The Android Open Source Project 31754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * 41754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * Licensed under the Apache License, Version 2.0 (the "License"); 51754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * you may not use this file except in compliance with the License. 61754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * You may obtain a copy of the License at 71754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * 81754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * http://www.apache.org/licenses/LICENSE-2.0 91754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * 101754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * Unless required by applicable law or agreed to in writing, software 111754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * distributed under the License is distributed on an "AS IS" BASIS, 121754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 131754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * See the License for the specific language governing permissions and 141754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato * limitations under the License. 151754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato */ 161754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 171754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#define LOG_TAG "incidentd" 181754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 191754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include "FdBuffer.h" 201754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 211754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include <cutils/log.h> 221754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include <utils/SystemClock.h> 231754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 241754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include <fcntl.h> 251754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include <poll.h> 261754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato#include <unistd.h> 270a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin#include <wait.h> 281754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 290eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jinconst bool DEBUG = false; 300a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jinconst ssize_t BUFFER_SIZE = 16 * 1024; // 16 KB 311754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onoratoconst ssize_t MAX_BUFFER_COUNT = 256; // 4 MB max 321754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 331754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe OnoratoFdBuffer::FdBuffer() 34c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin :mBuffer(BUFFER_SIZE), 351754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mStartTime(-1), 361754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mFinishTime(-1), 371754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mTimedOut(false), 381754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mTruncated(false) 391754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato{ 401754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato} 411754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 421754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe OnoratoFdBuffer::~FdBuffer() 431754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato{ 441754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato} 451754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 461754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onoratostatus_t 471754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe OnoratoFdBuffer::read(int fd, int64_t timeout) 481754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato{ 491754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato struct pollfd pfds = { 501754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato .fd = fd, 511754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato .events = POLLIN 521754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato }; 531754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mStartTime = uptimeMillis(); 541754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 551754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); 561754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 571754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato while (true) { 58c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { 59c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin mTruncated = true; 60c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin break; 611754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 62c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; 631754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 641754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato int64_t remainingTime = (mStartTime + timeout) - uptimeMillis(); 651754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato if (remainingTime <= 0) { 661754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mTimedOut = true; 671754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato break; 681754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 691754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 701754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato int count = poll(&pfds, 1, remainingTime); 711754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato if (count == 0) { 721754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mTimedOut = true; 731754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato break; 741754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } else if (count < 0) { 750eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("poll failed: %s", strerror(errno)); 761754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato return -errno; 771754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } else { 781754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato if ((pfds.revents & POLLERR) != 0) { 790eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("return event has error %s", strerror(errno)); 801754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato return errno != 0 ? -errno : UNKNOWN_ERROR; 811754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } else { 82c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin ssize_t amt = ::read(fd, mBuffer.writeBuffer(), mBuffer.currentToWrite()); 831754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato if (amt < 0) { 841754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato if (errno == EAGAIN || errno == EWOULDBLOCK) { 851754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato continue; 861754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } else { 870eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("Fail to read %d: %s", fd, strerror(errno)); 881754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato return -errno; 891754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 901754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } else if (amt == 0) { 911754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato break; 921754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 93c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin mBuffer.wp()->move(amt); 941754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 951754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 961754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato } 971754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato mFinishTime = uptimeMillis(); 981754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato return NO_ERROR; 991754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato} 1001754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato 1010a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jinstatus_t 1020eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi JinFdBuffer::readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs, const bool isSysfs) 1030a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin{ 1040a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin struct pollfd pfds[] = { 1050a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin { .fd = fd, .events = POLLIN }, 1060a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin { .fd = toFd, .events = POLLOUT }, 1070a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin { .fd = fromFd, .events = POLLIN }, 1080a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin }; 1090a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1100a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin mStartTime = uptimeMillis(); 1110a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1120a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // mark all fds non blocking 1130a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin fcntl(fd, F_SETFL, fcntl(fd, F_GETFL, 0) | O_NONBLOCK); 1140a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin fcntl(toFd, F_SETFL, fcntl(toFd, F_GETFL, 0) | O_NONBLOCK); 1150a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin fcntl(fromFd, F_SETFL, fcntl(fromFd, F_GETFL, 0) | O_NONBLOCK); 1160a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1170a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // A circular buffer holds data read from fd and writes to parsing process 1180a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin uint8_t cirBuf[BUFFER_SIZE]; 1190a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin size_t cirSize = 0; 1200a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin int rpos = 0, wpos = 0; 1210a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1220a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // This is the buffer used to store processed data 1230a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin while (true) { 124c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin if (mBuffer.size() >= MAX_BUFFER_COUNT * BUFFER_SIZE) { 125c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin mTruncated = true; 126c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin break; 1270a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 128c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin if (mBuffer.writeBuffer() == NULL) return NO_MEMORY; 1290a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1300a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin int64_t remainingTime = (mStartTime + timeoutMs) - uptimeMillis(); 1310a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (remainingTime <= 0) { 1320a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin mTimedOut = true; 1330a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin break; 1340a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1350a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1360a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // wait for any pfds to be ready to perform IO 1370a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin int count = poll(pfds, 3, remainingTime); 1380a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (count == 0) { 1390a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin mTimedOut = true; 1400a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin break; 1410a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else if (count < 0) { 1420eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("Fail to poll: %s", strerror(errno)); 1430a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return -errno; 1440a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1450a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1460a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // make sure no errors occur on any fds 1470a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin for (int i = 0; i < 3; ++i) { 1480a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if ((pfds[i].revents & POLLERR) != 0) { 1490eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (i == 0 && isSysfs) { 1500eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("fd %d is sysfs, ignore its POLLERR return value", fd); 1510eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin continue; 1520eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin } 1530eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("fd[%d]=%d returns error events: %s", i, fd, strerror(errno)); 1540a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return errno != 0 ? -errno : UNKNOWN_ERROR; 1550a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1560a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1570a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1580a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // read from fd 1590a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (cirSize != BUFFER_SIZE && pfds[0].fd != -1) { 1600a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin ssize_t amt; 1610a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (rpos >= wpos) { 1620a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin amt = ::read(fd, cirBuf + rpos, BUFFER_SIZE - rpos); 1630a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else { 1640ed9b68a3fa8f6eab536a93cb18ce75d7d22b757Yi Jin amt = ::read(fd, cirBuf + rpos, wpos - rpos); 1650a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1660a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (amt < 0) { 1670a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { 1680eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("Fail to read fd %d: %s", fd, strerror(errno)); 1690a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return -errno; 1700a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } // otherwise just continue 1710a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else if (amt == 0) { // reach EOF so don't have to poll pfds[0]. 1720a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin ::close(pfds[0].fd); 1730a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin pfds[0].fd = -1; 1740a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else { 1750a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin rpos += amt; 1760a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin cirSize += amt; 1770a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1780a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1790a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1800a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // write to parsing process 1810a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (cirSize > 0 && pfds[1].fd != -1) { 1820a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin ssize_t amt; 1830a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (rpos > wpos) { 1840a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin amt = ::write(toFd, cirBuf + wpos, rpos - wpos); 1850a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else { 1860a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin amt = ::write(toFd, cirBuf + wpos, BUFFER_SIZE - wpos); 1870a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1880a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (amt < 0) { 1890a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { 1900eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("Fail to write toFd %d: %s", toFd, strerror(errno)); 1910a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return -errno; 1920a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } // otherwise just continue 1930a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else { 1940a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin wpos += amt; 1950a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin cirSize -= amt; 1960a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1970a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 1980a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 1990a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // if buffer is empty and fd is closed, close write fd. 2000a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (cirSize == 0 && pfds[0].fd == -1 && pfds[1].fd != -1) { 2010a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin ::close(pfds[1].fd); 2020a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin pfds[1].fd = -1; 2030a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 2040a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 2050a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // circular buffer, reset rpos and wpos 2060a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (rpos >= BUFFER_SIZE) { 2070a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin rpos = 0; 2080a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 2090a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (wpos >= BUFFER_SIZE) { 2100a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin wpos = 0; 2110a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 2120a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 2130a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin // read from parsing process 214c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin ssize_t amt = ::read(fromFd, mBuffer.writeBuffer(), mBuffer.currentToWrite()); 2150a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (amt < 0) { 2160a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin if (!(errno == EAGAIN || errno == EWOULDBLOCK)) { 2170eb223496c3dee6bd0a33ea4fa664fb92ef557ccYi Jin if (DEBUG) ALOGD("Fail to read fromFd %d: %s", fromFd, strerror(errno)); 2180a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return -errno; 2190a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } // otherwise just continue 2200a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else if (amt == 0) { 2210a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin break; 2220a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } else { 223c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin mBuffer.wp()->move(amt); 2240a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 2250a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin } 2260a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 2270a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin mFinishTime = uptimeMillis(); 2280a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin return NO_ERROR; 2290a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin} 2300a3406fc4f8e9a8c8a9155fc7886a0496f692496Yi Jin 2311754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onoratosize_t 23299c248feb2d1f863b864bdfd1e3b37af17f18732Yi JinFdBuffer::size() const 2331754d744a7a34731ffc07af1bc3dbfcb06864ab0Joe Onorato{ 234c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin return mBuffer.size(); 2350ed9b68a3fa8f6eab536a93cb18ce75d7d22b757Yi Jin} 2360f0471623e91c202fb7381a050cc331572fb439fYi Jin 237c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi JinEncodedBuffer::iterator 238c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi JinFdBuffer::data() const 2390f0471623e91c202fb7381a050cc331572fb439fYi Jin{ 240c23fad2f9079f678eae15338f5e57e2a6bf7e391Yi Jin return mBuffer.begin(); 2410f0471623e91c202fb7381a050cc331572fb439fYi Jin} 242