• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #pragma once
17 
18 #ifndef FD_BUFFER_H
19 #define FD_BUFFER_H
20 
21 #include <android-base/unique_fd.h>
22 #include <android/util/EncodedBuffer.h>
23 #include <utils/Errors.h>
24 
25 namespace android {
26 namespace os {
27 namespace incidentd {
28 
29 using namespace android::base;
30 using namespace android::util;
31 
32 /**
33  * Reads data from fd into a buffer, fd must be closed explicitly.
34  */
35 class FdBuffer {
36 public:
37     FdBuffer();
38     ~FdBuffer();
39 
40     /**
41      * Read the data until the timeout is hit or we hit eof.
42      * Returns NO_ERROR if there were no errors or if we timed out.
43      * Will mark the file O_NONBLOCK.
44      */
45     status_t read(int fd, int64_t timeoutMs);
46 
47     /**
48      * Read the data until we hit eof.
49      * Returns NO_ERROR if there were no errors.
50      */
51     status_t readFully(int fd);
52 
53     /**
54      * Read processed results by streaming data to a parsing process, e.g. incident helper.
55      * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function
56      * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads
57      * and stores the processed data from 'fromFd' in memory for later usage.
58      * This function behaves in a streaming fashion in order to save memory usage.
59      * Returns NO_ERROR if there were no errors or if we timed out.
60      *
61      * Poll will return POLLERR if fd is from sysfs, handle this edge case.
62      */
63     status_t readProcessedDataInStream(int fd, unique_fd toFd, unique_fd fromFd, int64_t timeoutMs,
64                                        const bool isSysfs = false);
65 
66     /**
67      * Write by hand into the buffer.
68      */
69     status_t write(uint8_t const* buf, size_t size);
70 
71     /**
72      * Write all the data from a ProtoReader into our internal buffer.
73      */
74     status_t write(const sp<ProtoReader>& data);
75 
76     /**
77      * Write size bytes of data from a ProtoReader into our internal buffer.
78      */
79     status_t write(const sp<ProtoReader>& data, size_t size);
80 
81     /**
82      * Whether we timed out.
83      */
timedOut()84     bool timedOut() const { return mTimedOut; }
85 
86     /**
87      * If more than 4 MB is read, we truncate the data and return success.
88      * Downstream tools must handle truncated incident reports as best as possible
89      * anyway because they could be cut off for a lot of reasons and it's best
90      * to get as much useful information out of the system as possible. If this
91      * happens, truncated() will return true so it can be marked. If the data is
92      * exactly 4 MB, truncated is still set. Sorry.
93      */
truncated()94     bool truncated() const { return mTruncated; }
95 
96     /**
97      * How much data was read.
98      */
99     size_t size() const;
100 
101     /**
102      * How long the read took in milliseconds.
103      */
durationMs()104     int64_t durationMs() const { return mFinishTime - mStartTime; }
105 
106     /**
107      * Get the EncodedBuffer inside.
108      */
109     sp<EncodedBuffer> data() const;
110 
111 private:
112     sp<EncodedBuffer> mBuffer;
113     int64_t mStartTime;
114     int64_t mFinishTime;
115     bool mTimedOut;
116     bool mTruncated;
117 };
118 
119 }  // namespace incidentd
120 }  // namespace os
121 }  // namespace android
122 
123 #endif  // FD_BUFFER_H
124