blob: 03a6d1865036f4d1f9e397eb569c7e7833495794 [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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
27using namespace android;
28using namespace std;
29
30/**
31 * Reads a file into a buffer, and then writes that data to an FdSet.
32 */
33class FdBuffer
34{
35public:
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 Jin0a3406f2017-06-22 19:23:11 -070047 * 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 Onorato1754d742016-11-21 17:51:35 -080057 * 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
86private:
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 Jin0a3406f2017-06-22 19:23:11 -070095class Fpipe {
96public:
97 Fpipe() {}
98 bool close() { return !(::close(mFds[0]) || ::close(mFds[1])); }
99 ~Fpipe() { close(); }
100
Yi Jinb44f7d42017-07-21 12:12:59 -0700101 inline bool init() { return pipe(mFds) != -1; }
Yi Jin0a3406f2017-06-22 19:23:11 -0700102 inline int readFd() const { return mFds[0]; }
103 inline int writeFd() const { return mFds[1]; }
104
105private:
106 int mFds[2];
107};
108
Joe Onorato1754d742016-11-21 17:51:35 -0800109
110#endif // FD_BUFFER_H