blob: bd47a23d369e27c223b61c3a3ca4c57669fc3c7b [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
Mike Mab6f7c472020-03-03 17:58:35 -080018#include "incidentd_util.h"
Joe Onorato99598ee2019-02-11 15:55:13 +000019#include "FdBuffer.h"
Joe Onorato99598ee2019-02-11 15:55:13 +000020#include "WorkDirectory.h"
Joe Onorato1754d742016-11-21 17:51:35 -080021
Joe Onorato99598ee2019-02-11 15:55:13 +000022#include "frameworks/base/core/proto/android/os/metadata.pb.h"
23#include <android/content/ComponentName.h>
Joe Onorato1754d742016-11-21 17:51:35 -080024#include <android/os/IIncidentReportStatusListener.h>
Mike Ma643de922019-12-17 10:56:17 -080025#include <android/os/IIncidentDumpCallback.h>
Joe Onorato1754d742016-11-21 17:51:35 -080026#include <android/os/IncidentReportArgs.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000027#include <android/util/protobuf.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028
Yi Jin329130b2018-02-09 16:47:47 -080029#include <map>
Joe Onorato1754d742016-11-21 17:51:35 -080030#include <string>
31#include <vector>
32
33#include <time.h>
Joe Onorato99598ee2019-02-11 15:55:13 +000034#include <stdarg.h>
Yi Jin4e843102018-02-14 15:36:18 -080035
Yi Jin6cacbcb2018-03-30 14:04:52 -070036namespace android {
37namespace os {
38namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080039
Joe Onorato99598ee2019-02-11 15:55:13 +000040using namespace std;
41using namespace android::content;
42using namespace android::os;
43
Mike Ma643de922019-12-17 10:56:17 -080044class BringYourOwnSection;
Joe Onorato99598ee2019-02-11 15:55:13 +000045class Section;
46
Joe Onorato1754d742016-11-21 17:51:35 -080047// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +000048class ReportRequest : public virtual RefBase {
49public:
Joe Onorato1754d742016-11-21 17:51:35 -080050 IncidentReportArgs args;
Joe Onorato1754d742016-11-21 17:51:35 -080051
Yi Jinb592e3b2018-02-01 15:17:04 -080052 ReportRequest(const IncidentReportArgs& args, const sp<IIncidentReportStatusListener>& listener,
53 int fd);
Joe Onorato1754d742016-11-21 17:51:35 -080054 virtual ~ReportRequest();
Yi Jinedfd5bb2017-09-06 17:09:11 -070055
Joe Onorato99598ee2019-02-11 15:55:13 +000056 bool isStreaming() { return mIsStreaming; }
57
58 void setStatus(status_t err) { mStatus = err; }
59 status_t getStatus() const { return mStatus; }
60
Yi Jinb592e3b2018-02-01 15:17:04 -080061 bool ok(); // returns true if the request is ok for write.
Joe Onorato99598ee2019-02-11 15:55:13 +000062
Joe Onorato7a406b42019-04-12 18:11:30 -070063 bool containsSection(int sectionId) const;
Joe Onorato99598ee2019-02-11 15:55:13 +000064
65 sp<IIncidentReportStatusListener> getListener() { return mListener; }
66
Mike Mab6f7c472020-03-03 17:58:35 -080067 int getFd();
Joe Onorato99598ee2019-02-11 15:55:13 +000068
69 int setPersistedFd(int fd);
70
Mike Mab6f7c472020-03-03 17:58:35 -080071 status_t initGzipIfNecessary();
72
Joe Onorato99598ee2019-02-11 15:55:13 +000073 void closeFd();
74
75private:
76 sp<IIncidentReportStatusListener> mListener;
77 int mFd;
78 bool mIsStreaming;
79 status_t mStatus;
Mike Mab6f7c472020-03-03 17:58:35 -080080 pid_t mZipPid;
81 Fpipe mZipPipe;
Joe Onorato1754d742016-11-21 17:51:35 -080082};
83
84// ================================================================================
Joe Onorato99598ee2019-02-11 15:55:13 +000085class ReportBatch : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -080086public:
Joe Onorato99598ee2019-02-11 15:55:13 +000087 ReportBatch();
88 virtual ~ReportBatch();
Joe Onorato1754d742016-11-21 17:51:35 -080089
Joe Onorato99598ee2019-02-11 15:55:13 +000090 // TODO: Should there be some kind of listener associated with the
91 // component? Could be good for getting status updates e.g. in the ui,
92 // as it progresses. But that's out of scope for now.
Joe Onorato1754d742016-11-21 17:51:35 -080093
Joe Onorato99598ee2019-02-11 15:55:13 +000094 /**
95 * Schedule a report for the "main" report, where it will be delivered to
96 * the uploaders and/or dropbox.
97 */
98 void addPersistedReport(const IncidentReportArgs& args);
Joe Onorato1754d742016-11-21 17:51:35 -080099
Joe Onorato99598ee2019-02-11 15:55:13 +0000100 /**
101 * Adds a ReportRequest to the queue for one that has a listener an and fd
102 */
103 void addStreamingReport(const IncidentReportArgs& args,
104 const sp<IIncidentReportStatusListener>& listener, int streamFd);
Joe Onorato1754d742016-11-21 17:51:35 -0800105
Joe Onorato99598ee2019-02-11 15:55:13 +0000106 /**
107 * Returns whether both queues are empty.
108 */
109 bool empty() const;
Yi Jin329130b2018-02-09 16:47:47 -0800110
Joe Onorato99598ee2019-02-11 15:55:13 +0000111 /**
112 * Returns whether there are any persisted records.
113 */
114 bool hasPersistedReports() const { return mPersistedRequests.size() > 0; }
115
116 /**
117 * Return the persisted request for the given component, or nullptr.
118 */
119 sp<ReportRequest> getPersistedRequest(const ComponentName& component);
120
121 /**
122 * Call func(request) for each Request.
123 */
124 void forEachPersistedRequest(const function<void (const sp<ReportRequest>&)>& func);
125
126 /**
127 * Call func(request) for each Request.
128 */
129 void forEachStreamingRequest(const function<void (const sp<ReportRequest>&)>& func);
130
131 /**
Mike Ma643de922019-12-17 10:56:17 -0800132 * Call func(request) for each file descriptor.
Joe Onorato99598ee2019-02-11 15:55:13 +0000133 */
134 void forEachFd(int sectionId, const function<void (const sp<ReportRequest>&)>& func);
135
136 /**
137 * Call func(listener) for every listener in this batch.
138 */
139 void forEachListener(const function<void (const sp<IIncidentReportStatusListener>&)>& func);
140
141 /**
142 * Call func(listener) for every listener in this batch that requests
143 * sectionId.
144 */
145 void forEachListener(int sectionId,
146 const function<void (const sp<IIncidentReportStatusListener>&)>& func);
147 /**
148 * Get an IncidentReportArgs that represents the combined args for the
149 * persisted requests.
150 */
151 void getCombinedPersistedArgs(IncidentReportArgs* results);
152
153 /**
154 * Return whether any of the requests contain the section.
155 */
Yi Jin329130b2018-02-09 16:47:47 -0800156 bool containsSection(int id);
Joe Onorato99598ee2019-02-11 15:55:13 +0000157
158 /**
159 * Remove all of the broadcast (persisted) requests.
160 */
161 void clearPersistedRequests();
162
163 /**
Joe Onoratoe5472052019-04-24 16:27:33 -0700164 * Move the streaming requests in this batch to that batch. After this call there
165 * will be no streaming requests in this batch.
166 */
167 void transferStreamingRequests(const sp<ReportBatch>& that);
168
169 /**
170 * Move the persisted requests in this batch to that batch. After this call there
171 * will be no streaming requests in this batch.
172 */
173 void transferPersistedRequests(const sp<ReportBatch>& that);
174
175 /**
Joe Onorato99598ee2019-02-11 15:55:13 +0000176 * Get the requests that have encountered errors.
177 */
178 void getFailedRequests(vector<sp<ReportRequest>>* requests);
179
180 /**
181 * Remove the request from whichever list it's in.
182 */
183 void removeRequest(const sp<ReportRequest>& request);
184
Yi Jin329130b2018-02-09 16:47:47 -0800185
Joe Onorato1754d742016-11-21 17:51:35 -0800186private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000187 map<ComponentName, sp<ReportRequest>> mPersistedRequests;
188 vector<sp<ReportRequest>> mStreamingRequests;
189};
Yi Jin329130b2018-02-09 16:47:47 -0800190
Joe Onorato99598ee2019-02-11 15:55:13 +0000191// ================================================================================
192class ReportWriter {
193public:
194 ReportWriter(const sp<ReportBatch>& batch);
195 ~ReportWriter();
196
197 void setPersistedFile(sp<ReportFile> file);
198 void setMaxPersistedPrivacyPolicy(uint8_t privacyPolicy);
199
200 void startSection(int sectionId);
201 void endSection(IncidentMetadata::SectionStats* sectionStats);
202
203 void setSectionStats(const FdBuffer& buffer);
204
205 void warning(const Section* section, status_t err, const char* format, ...);
206 void error(const Section* section, status_t err, const char* format, ...);
207
208 status_t writeSection(const FdBuffer& buffer);
209
210private:
211 // Data about all requests
212 sp<ReportBatch> mBatch;
213
214 /**
215 * The file on disk where we will store the persisted file.
216 */
217 sp<ReportFile> mPersistedFile;
218
219 /**
220 * The least restricted privacy policy of all of the perstited
221 * requests. We pre-filter to that to save disk space.
222 */
223 uint8_t mMaxPersistedPrivacyPolicy;
224
225 /**
226 * The current section that is being written.
227 */
228 int mCurrentSectionId;
229
230 /**
231 * The time that that the current section was started.
232 */
233 int64_t mSectionStartTimeMs;
234
235 /**
236 * The last section that setSectionStats was called for, so if someone misses
237 * it we can log that.
238 */
239 int mSectionStatsCalledForSectionId;
240
241 /*
242 * Fields for IncidentMetadata.SectionStats. Set by setSectionStats. Accessed by
243 * getSectionStats.
244 */
245 int32_t mDumpSizeBytes;
246 int64_t mDumpDurationMs;
247 bool mSectionTimedOut;
248 bool mSectionTruncated;
249 bool mSectionBufferSuccess;
250 bool mHadError;
251 string mSectionErrors;
252 size_t mMaxSectionDataFilteredSize;
253
254 void vflog(const Section* section, status_t err, int level, const char* levelText,
255 const char* format, va_list args);
Joe Onorato1754d742016-11-21 17:51:35 -0800256};
257
258// ================================================================================
Yi Jinb592e3b2018-02-01 15:17:04 -0800259class Reporter : public virtual RefBase {
Joe Onorato1754d742016-11-21 17:51:35 -0800260public:
Mike Ma643de922019-12-17 10:56:17 -0800261 Reporter(const sp<WorkDirectory>& workDirectory,
262 const sp<ReportBatch>& batch,
263 const vector<BringYourOwnSection*>& registeredSections);
Joe Onorato1754d742016-11-21 17:51:35 -0800264
Joe Onorato1754d742016-11-21 17:51:35 -0800265 virtual ~Reporter();
266
267 // Run the report as described in the batch and args parameters.
Joe Onorato99598ee2019-02-11 15:55:13 +0000268 void runReport(size_t* reportByteSize);
Joe Onorato1754d742016-11-21 17:51:35 -0800269
270private:
Joe Onorato99598ee2019-02-11 15:55:13 +0000271 sp<WorkDirectory> mWorkDirectory;
272 ReportWriter mWriter;
273 sp<ReportBatch> mBatch;
274 sp<ReportFile> mPersistedFile;
Mike Ma643de922019-12-17 10:56:17 -0800275 const vector<BringYourOwnSection*>& mRegisteredSections;
276
277 status_t execute_section(const Section* section, IncidentMetadata* metadata,
278 size_t* reportByteSize);
Yi Jinadd11e92017-07-30 16:10:07 -0700279
Joe Onorato99598ee2019-02-11 15:55:13 +0000280 void cancel_and_remove_failed_requests();
Joe Onorato1754d742016-11-21 17:51:35 -0800281};
282
Yi Jin6cacbcb2018-03-30 14:04:52 -0700283} // namespace incidentd
284} // namespace os
285} // namespace android