blob: 34a3613cf0d060f9cfbe54278f8234c1e8023b95 [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 SECTIONS_H
19#define SECTIONS_H
20
Yi Jin99c248f2017-08-25 18:11:58 -070021#include "Reporter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080022
Yi Jinb44f7d42017-07-21 12:12:59 -070023#include <stdarg.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080024#include <map>
Yi Jin3c034c92017-12-22 17:36:47 -080025
Joe Onorato1754d742016-11-21 17:51:35 -080026#include <utils/String16.h>
Yi Jinb592e3b2018-02-01 15:17:04 -080027#include <utils/String8.h>
Joe Onorato1754d742016-11-21 17:51:35 -080028#include <utils/Vector.h>
29
Yi Jin6cacbcb2018-03-30 14:04:52 -070030namespace android {
31namespace os {
32namespace incidentd {
Joe Onorato1754d742016-11-21 17:51:35 -080033
Yi Jinb592e3b2018-02-01 15:17:04 -080034const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds
Yi Jinb44f7d42017-07-21 12:12:59 -070035
Joe Onorato1754d742016-11-21 17:51:35 -080036/**
37 * Base class for sections
38 */
Yi Jinb592e3b2018-02-01 15:17:04 -080039class Section {
Joe Onorato1754d742016-11-21 17:51:35 -080040public:
Yi Jinb44f7d42017-07-21 12:12:59 -070041 const int id;
Yi Jinb592e3b2018-02-01 15:17:04 -080042 const int64_t timeoutMs; // each section must have a timeout
Joe Onorato1754d742016-11-21 17:51:35 -080043 String8 name;
44
Yi Jinb44f7d42017-07-21 12:12:59 -070045 Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080046 virtual ~Section();
47
48 virtual status_t Execute(ReportRequestSet* requests) const = 0;
Joe Onorato1754d742016-11-21 17:51:35 -080049};
50
51/**
Yi Jinedfd5bb2017-09-06 17:09:11 -070052 * Section that generates incident headers.
53 */
Yi Jinb592e3b2018-02-01 15:17:04 -080054class HeaderSection : public Section {
Yi Jinedfd5bb2017-09-06 17:09:11 -070055public:
56 HeaderSection();
57 virtual ~HeaderSection();
58
59 virtual status_t Execute(ReportRequestSet* requests) const;
60};
61
62/**
Yi Jin329130b2018-02-09 16:47:47 -080063 * Section that generates incident metadata.
64 */
Yi Jinb592e3b2018-02-01 15:17:04 -080065class MetadataSection : public Section {
Yi Jin329130b2018-02-09 16:47:47 -080066public:
67 MetadataSection();
68 virtual ~MetadataSection();
69
70 virtual status_t Execute(ReportRequestSet* requests) const;
71};
72
73/**
Joe Onorato1754d742016-11-21 17:51:35 -080074 * Section that reads in a file.
75 */
Yi Jinb592e3b2018-02-01 15:17:04 -080076class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080077public:
Yi Jinb44f7d42017-07-21 12:12:59 -070078 FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080079 virtual ~FileSection();
80
81 virtual status_t Execute(ReportRequestSet* requests) const;
82
83private:
84 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080085 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080086};
87
88/**
Yi Jin1a11fa12018-02-22 16:44:10 -080089 * Section that reads in a file and gzips the content.
90 */
91class GZipSection : public Section {
92public:
93 GZipSection(int id, const char* filename, ...);
94 virtual ~GZipSection();
95
96 virtual status_t Execute(ReportRequestSet* requests) const;
97
98private:
99 // It looks up the content from multiple files and stops when the first one is available.
100 const char** mFilenames;
101};
102
103/**
Joe Onorato1754d742016-11-21 17:51:35 -0800104 * Base class for sections that call a command that might need a timeout.
105 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800106class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800107public:
Kweku Adamseadd1232018-02-05 16:45:13 -0800108 WorkerThreadSection(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -0800109 virtual ~WorkerThreadSection();
110
111 virtual status_t Execute(ReportRequestSet* requests) const;
112
113 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
114};
115
116/**
117 * Section that forks and execs a command, and puts stdout as the section.
118 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800119class CommandSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800120public:
Yi Jinb44f7d42017-07-21 12:12:59 -0700121 CommandSection(int id, const int64_t timeoutMs, const char* command, ...);
122
123 CommandSection(int id, const char* command, ...);
124
Joe Onorato1754d742016-11-21 17:51:35 -0800125 virtual ~CommandSection();
126
127 virtual status_t Execute(ReportRequestSet* requests) const;
128
129private:
130 const char** mCommand;
Joe Onorato1754d742016-11-21 17:51:35 -0800131};
132
133/**
134 * Section that calls dumpsys on a system service.
135 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800136class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800137public:
138 DumpsysSection(int id, const char* service, ...);
139 virtual ~DumpsysSection();
140
141 virtual status_t BlockingCall(int pipeWriteFd) const;
142
143private:
144 String16 mService;
145 Vector<String16> mArgs;
146};
147
Yi Jin3c034c92017-12-22 17:36:47 -0800148/**
149 * Section that reads from logd.
150 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800151class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800152 // global last log retrieved timestamp for each log_id_t.
153 static map<log_id_t, log_time> gLastLogsRetrieved;
154
155public:
156 LogSection(int id, log_id_t logID);
157 virtual ~LogSection();
158
159 virtual status_t BlockingCall(int pipeWriteFd) const;
160
161private:
162 log_id_t mLogID;
163 bool mBinary;
164};
165
Kweku Adamseadd1232018-02-05 16:45:13 -0800166/**
167 * Section that gets data from tombstoned.
168 */
169class TombstoneSection : public WorkerThreadSection {
170public:
171 TombstoneSection(int id, const char* type, const int64_t timeoutMs = 30000 /* 30 seconds */);
172 virtual ~TombstoneSection();
173
174 virtual status_t BlockingCall(int pipeWriteFd) const;
175
176private:
177 std::string mType;
178};
179
Yi Jin6cacbcb2018-03-30 14:04:52 -0700180} // namespace incidentd
181} // namespace os
182} // namespace android
183
Yi Jinb592e3b2018-02-01 15:17:04 -0800184#endif // SECTIONS_H