blob: 66a3de154c5198f889665604613bb39b4dd584e3 [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 */
Yi Jinb592e3b2018-02-01 15:17:04 -080016#pragma once
Joe Onorato1754d742016-11-21 17:51:35 -080017
18#ifndef FD_BUFFER_H
19#define FD_BUFFER_H
20
Yi Jinc23fad22017-09-15 17:24:59 -070021#include <android/util/EncodedBuffer.h>
Joe Onorato1754d742016-11-21 17:51:35 -080022#include <utils/Errors.h>
23
Joe Onorato1754d742016-11-21 17:51:35 -080024using namespace android;
Yi Jinc23fad22017-09-15 17:24:59 -070025using namespace android::util;
Joe Onorato1754d742016-11-21 17:51:35 -080026using namespace std;
27
28/**
Yi Jin1a11fa12018-02-22 16:44:10 -080029 * Reads data from fd into a buffer, fd must be closed explicitly.
Joe Onorato1754d742016-11-21 17:51:35 -080030 */
Yi Jinb592e3b2018-02-01 15:17:04 -080031class FdBuffer {
Joe Onorato1754d742016-11-21 17:51:35 -080032public:
33 FdBuffer();
34 ~FdBuffer();
35
36 /**
37 * Read the data until the timeout is hit or we hit eof.
38 * Returns NO_ERROR if there were no errors or if we timed out.
39 * Will mark the file O_NONBLOCK.
40 */
41 status_t read(int fd, int64_t timeoutMs);
42
43 /**
Yi Jin0a3406f2017-06-22 19:23:11 -070044 * Read processed results by streaming data to a parsing process, e.g. incident helper.
45 * The parsing process provides IO fds which are 'toFd' and 'fromFd'. The function
46 * reads original data in 'fd' and writes to parsing process through 'toFd', then it reads
47 * and stores the processed data from 'fromFd' in memory for later usage.
48 * This function behaves in a streaming fashion in order to save memory usage.
49 * Returns NO_ERROR if there were no errors or if we timed out.
Yi Jin0eb22342017-11-06 17:17:27 -080050 *
51 * Poll will return POLLERR if fd is from sysfs, handle this edge case.
Yi Jin0a3406f2017-06-22 19:23:11 -070052 */
Yi Jinb592e3b2018-02-01 15:17:04 -080053 status_t readProcessedDataInStream(int fd, int toFd, int fromFd, int64_t timeoutMs,
54 const bool isSysfs = false);
Yi Jin0a3406f2017-06-22 19:23:11 -070055
56 /**
Joe Onorato1754d742016-11-21 17:51:35 -080057 * Whether we timed out.
58 */
Yi Jin99c248f2017-08-25 18:11:58 -070059 bool timedOut() const { return mTimedOut; }
Joe Onorato1754d742016-11-21 17:51:35 -080060
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 */
Yi Jin99c248f2017-08-25 18:11:58 -070069 bool truncated() const { return mTruncated; }
Joe Onorato1754d742016-11-21 17:51:35 -080070
71 /**
72 * How much data was read.
73 */
Yi Jin99c248f2017-08-25 18:11:58 -070074 size_t size() const;
Joe Onorato1754d742016-11-21 17:51:35 -080075
76 /**
Joe Onorato1754d742016-11-21 17:51:35 -080077 * How long the read took in milliseconds.
78 */
Yi Jin99c248f2017-08-25 18:11:58 -070079 int64_t durationMs() const { return mFinishTime - mStartTime; }
Joe Onorato1754d742016-11-21 17:51:35 -080080
Yi Jin0ed9b682017-08-18 14:51:20 -070081 /**
Yi Jinc23fad22017-09-15 17:24:59 -070082 * Reader API for data stored in FdBuffer
Yi Jin0ed9b682017-08-18 14:51:20 -070083 */
Yi Jinc23fad22017-09-15 17:24:59 -070084 EncodedBuffer::iterator data() const;
Yi Jin0ed9b682017-08-18 14:51:20 -070085
Yi Jin1a11fa12018-02-22 16:44:10 -080086 /**
87 * Return the internal buffer, don't call unless you are familiar with EncodedBuffer.
88 */
89 EncodedBuffer* getInternalBuffer() { return &mBuffer; }
90
Joe Onorato1754d742016-11-21 17:51:35 -080091private:
Yi Jinc23fad22017-09-15 17:24:59 -070092 EncodedBuffer mBuffer;
Joe Onorato1754d742016-11-21 17:51:35 -080093 int64_t mStartTime;
94 int64_t mFinishTime;
Joe Onorato1754d742016-11-21 17:51:35 -080095 bool mTimedOut;
96 bool mTruncated;
97};
98
Yi Jinb592e3b2018-02-01 15:17:04 -080099#endif // FD_BUFFER_H