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