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 | #pragma once |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 17 | |
| 18 | #ifndef FD_BUFFER_H |
| 19 | #define FD_BUFFER_H |
| 20 | |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 21 | #include <android-base/unique_fd.h> |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 22 | #include <android/util/EncodedBuffer.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | #include <utils/Errors.h> |
| 24 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | using namespace android; |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 26 | using namespace android::base; |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 27 | using namespace android::util; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 28 | using namespace std; |
| 29 | |
| 30 | /** |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 31 | * Reads data from fd into a buffer, fd must be closed explicitly. |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 32 | */ |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 33 | class FdBuffer { |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 34 | public: |
| 35 | FdBuffer(); |
| 36 | ~FdBuffer(); |
| 37 | |
| 38 | /** |
| 39 | * Read the data until the timeout is hit or we hit eof. |
| 40 | * Returns NO_ERROR if there were no errors or if we timed out. |
| 41 | * Will mark the file O_NONBLOCK. |
| 42 | */ |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 43 | status_t read(unique_fd* fd, int64_t timeoutMs); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 44 | |
| 45 | /** |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 46 | * Read the data until we hit eof. |
| 47 | * Returns NO_ERROR if there were no errors. |
| 48 | */ |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 49 | status_t readFully(unique_fd* fd); |
Kweku Adams | eadd123 | 2018-02-05 16:45:13 -0800 | [diff] [blame] | 50 | |
| 51 | /** |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 52 | * Read processed results by streaming data to a parsing process, e.g. incident helper. |
| 53 | * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function |
| 54 | * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads |
| 55 | * and stores the processed data from 'fromFd' in memory for later usage. |
| 56 | * This function behaves in a streaming fashion in order to save memory usage. |
| 57 | * Returns NO_ERROR if there were no errors or if we timed out. |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 58 | * |
| 59 | * Poll will return POLLERR if fd is from sysfs, handle this edge case. |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 60 | */ |
Yi Jin | 6355d2f | 2018-03-14 15:18:02 -0700 | [diff] [blame] | 61 | status_t readProcessedDataInStream(unique_fd* fd, unique_fd* toFd, unique_fd* fromFd, |
| 62 | int64_t timeoutMs, const bool isSysfs = false); |
Yi Jin | 0a3406f | 2017-06-22 19:23:11 -0700 | [diff] [blame] | 63 | |
| 64 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 65 | * Whether we timed out. |
| 66 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 67 | bool timedOut() const { return mTimedOut; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 68 | |
| 69 | /** |
| 70 | * If more than 4 MB is read, we truncate the data and return success. |
| 71 | * Downstream tools must handle truncated incident reports as best as possible |
| 72 | * anyway because they could be cut off for a lot of reasons and it's best |
| 73 | * to get as much useful information out of the system as possible. If this |
| 74 | * happens, truncated() will return true so it can be marked. If the data is |
| 75 | * exactly 4 MB, truncated is still set. Sorry. |
| 76 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 77 | bool truncated() const { return mTruncated; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 78 | |
| 79 | /** |
| 80 | * How much data was read. |
| 81 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 82 | size_t size() const; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 83 | |
| 84 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 85 | * How long the read took in milliseconds. |
| 86 | */ |
Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 87 | int64_t durationMs() const { return mFinishTime - mStartTime; } |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 88 | |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 89 | /** |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 90 | * Reader API for data stored in FdBuffer |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 91 | */ |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 92 | EncodedBuffer::iterator data() const; |
Yi Jin | 0ed9b68 | 2017-08-18 14:51:20 -0700 | [diff] [blame] | 93 | |
Yi Jin | 1a11fa1 | 2018-02-22 16:44:10 -0800 | [diff] [blame] | 94 | /** |
| 95 | * Return the internal buffer, don't call unless you are familiar with EncodedBuffer. |
| 96 | */ |
| 97 | EncodedBuffer* getInternalBuffer() { return &mBuffer; } |
| 98 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | private: |
Yi Jin | c23fad2 | 2017-09-15 17:24:59 -0700 | [diff] [blame] | 100 | EncodedBuffer mBuffer; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 101 | int64_t mStartTime; |
| 102 | int64_t mFinishTime; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 103 | bool mTimedOut; |
| 104 | bool mTruncated; |
| 105 | }; |
| 106 | |
Yi Jin | b592e3b | 2018-02-01 15:17:04 -0800 | [diff] [blame] | 107 | #endif // FD_BUFFER_H |