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 | */ |
| 16 | |
| 17 | #ifndef FD_BUFFER_H |
| 18 | #define FD_BUFFER_H |
| 19 | |
| 20 | #include "Reporter.h" |
| 21 | |
| 22 | #include <utils/Errors.h> |
| 23 | |
| 24 | #include <set> |
| 25 | #include <vector> |
| 26 | |
| 27 | using namespace android; |
| 28 | using namespace std; |
| 29 | |
| 30 | /** |
| 31 | * Reads a file into a buffer, and then writes that data to an FdSet. |
| 32 | */ |
| 33 | class FdBuffer |
| 34 | { |
| 35 | public: |
| 36 | FdBuffer(); |
| 37 | ~FdBuffer(); |
| 38 | |
| 39 | /** |
| 40 | * Read the data until the timeout is hit or we hit eof. |
| 41 | * Returns NO_ERROR if there were no errors or if we timed out. |
| 42 | * Will mark the file O_NONBLOCK. |
| 43 | */ |
| 44 | status_t read(int fd, int64_t timeoutMs); |
| 45 | |
| 46 | /** |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 47 | * Read processed results by streaming data to a parsing process, e.g. incident helper. |
| 48 | * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function |
| 49 | * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads |
| 50 | * and stores the processed data from 'fromFd' in memory for later usage. |
| 51 | * This function behaves in a streaming fashion in order to save memory usage. |
| 52 | * Returns NO_ERROR if there were no errors or if we timed out. |
| 53 | */ |
| 54 | status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs); |
| 55 | |
| 56 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 57 | * Whether we timed out. |
| 58 | */ |
| 59 | bool timedOut() { return mTimedOut; } |
| 60 | |
| 61 | /** |
| 62 | * If more than 4 MB is read, we truncate the data and return success. |
| 63 | * Downstream tools must handle truncated incident reports as best as possible |
| 64 | * anyway because they could be cut off for a lot of reasons and it's best |
| 65 | * to get as much useful information out of the system as possible. If this |
| 66 | * happens, truncated() will return true so it can be marked. If the data is |
| 67 | * exactly 4 MB, truncated is still set. Sorry. |
| 68 | */ |
| 69 | bool truncated() { return mTruncated; } |
| 70 | |
| 71 | /** |
| 72 | * How much data was read. |
| 73 | */ |
| 74 | size_t size(); |
| 75 | |
| 76 | /** |
| 77 | * Write the data that we recorded to the fd given. |
| 78 | */ |
| 79 | status_t write(ReportRequestSet* requests); |
| 80 | |
| 81 | /** |
| 82 | * How long the read took in milliseconds. |
| 83 | */ |
| 84 | int64_t durationMs() { return mFinishTime - mStartTime; } |
| 85 | |
| 86 | private: |
| 87 | vector<uint8_t*> mBuffers; |
| 88 | int64_t mStartTime; |
| 89 | int64_t mFinishTime; |
| 90 | ssize_t mCurrentWritten; |
| 91 | bool mTimedOut; |
| 92 | bool mTruncated; |
| 93 | }; |
| 94 | |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame^] | 95 | class Fpipe { |
| 96 | public: |
| 97 | Fpipe() {} |
| 98 | bool close() { return !(::close(mFds[0]) || ::close(mFds[1])); } |
| 99 | ~Fpipe() { close(); } |
| 100 | |
| 101 | inline status_t init() { return pipe(mFds); } |
| 102 | inline int readFd() const { return mFds[0]; } |
| 103 | inline int writeFd() const { return mFds[1]; } |
| 104 | |
| 105 | private: |
| 106 | int mFds[2]; |
| 107 | }; |
| 108 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 109 | |
| 110 | #endif // FD_BUFFER_H |