blob: a031a15fe7c9a2bfcaf465f74d113ab69d4c2f7f [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 Jinad3e6e52018-04-03 15:10:34 -070034const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000; // 30 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
Kweku Adams3d160912018-05-07 11:26:27 -070043 const bool userdebugAndEngOnly;
Yi Jin3f360352018-04-16 16:13:04 -070044 const bool deviceSpecific;
Joe Onorato1754d742016-11-21 17:51:35 -080045 String8 name;
46
Kweku Adams3d160912018-05-07 11:26:27 -070047 Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS, bool userdebugAndEngOnly = false,
48 bool deviceSpecific = false);
Joe Onorato1754d742016-11-21 17:51:35 -080049 virtual ~Section();
50
51 virtual status_t Execute(ReportRequestSet* requests) const = 0;
Joe Onorato1754d742016-11-21 17:51:35 -080052};
53
54/**
Yi Jinedfd5bb2017-09-06 17:09:11 -070055 * Section that generates incident headers.
56 */
Yi Jinb592e3b2018-02-01 15:17:04 -080057class HeaderSection : public Section {
Yi Jinedfd5bb2017-09-06 17:09:11 -070058public:
59 HeaderSection();
60 virtual ~HeaderSection();
61
62 virtual status_t Execute(ReportRequestSet* requests) const;
63};
64
65/**
Yi Jin329130b2018-02-09 16:47:47 -080066 * Section that generates incident metadata.
67 */
Yi Jinb592e3b2018-02-01 15:17:04 -080068class MetadataSection : public Section {
Yi Jin329130b2018-02-09 16:47:47 -080069public:
70 MetadataSection();
71 virtual ~MetadataSection();
72
73 virtual status_t Execute(ReportRequestSet* requests) const;
74};
75
76/**
Joe Onorato1754d742016-11-21 17:51:35 -080077 * Section that reads in a file.
78 */
Yi Jinb592e3b2018-02-01 15:17:04 -080079class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080080public:
Yi Jin3f360352018-04-16 16:13:04 -070081 FileSection(int id, const char* filename, bool deviceSpecific = false,
82 int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080083 virtual ~FileSection();
84
85 virtual status_t Execute(ReportRequestSet* requests) const;
86
87private:
88 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080089 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080090};
91
92/**
Yi Jin1a11fa12018-02-22 16:44:10 -080093 * Section that reads in a file and gzips the content.
94 */
95class GZipSection : public Section {
96public:
97 GZipSection(int id, const char* filename, ...);
98 virtual ~GZipSection();
99
100 virtual status_t Execute(ReportRequestSet* requests) const;
101
102private:
103 // It looks up the content from multiple files and stops when the first one is available.
104 const char** mFilenames;
105};
106
107/**
Joe Onorato1754d742016-11-21 17:51:35 -0800108 * Base class for sections that call a command that might need a timeout.
109 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800110class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800111public:
Kweku Adams3d160912018-05-07 11:26:27 -0700112 WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS,
113 bool userdebugAndEngOnly = false);
Joe Onorato1754d742016-11-21 17:51:35 -0800114 virtual ~WorkerThreadSection();
115
116 virtual status_t Execute(ReportRequestSet* requests) const;
117
118 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
119};
120
121/**
122 * Section that forks and execs a command, and puts stdout as the section.
123 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800124class CommandSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -0800125public:
Yi Jin3f360352018-04-16 16:13:04 -0700126 CommandSection(int id, int64_t timeoutMs, const char* command, ...);
Yi Jinb44f7d42017-07-21 12:12:59 -0700127
128 CommandSection(int id, const char* command, ...);
129
Joe Onorato1754d742016-11-21 17:51:35 -0800130 virtual ~CommandSection();
131
132 virtual status_t Execute(ReportRequestSet* requests) const;
133
134private:
135 const char** mCommand;
Joe Onorato1754d742016-11-21 17:51:35 -0800136};
137
138/**
139 * Section that calls dumpsys on a system service.
140 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800141class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800142public:
Kweku Adams3d160912018-05-07 11:26:27 -0700143 DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...);
Joe Onorato1754d742016-11-21 17:51:35 -0800144 virtual ~DumpsysSection();
145
146 virtual status_t BlockingCall(int pipeWriteFd) const;
147
148private:
149 String16 mService;
150 Vector<String16> mArgs;
151};
152
Yi Jin3c034c92017-12-22 17:36:47 -0800153/**
154 * Section that reads from logd.
155 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800156class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800157 // global last log retrieved timestamp for each log_id_t.
158 static map<log_id_t, log_time> gLastLogsRetrieved;
159
160public:
161 LogSection(int id, log_id_t logID);
162 virtual ~LogSection();
163
164 virtual status_t BlockingCall(int pipeWriteFd) const;
165
166private:
167 log_id_t mLogID;
168 bool mBinary;
169};
170
Kweku Adamseadd1232018-02-05 16:45:13 -0800171/**
172 * Section that gets data from tombstoned.
173 */
174class TombstoneSection : public WorkerThreadSection {
175public:
Yi Jin3f360352018-04-16 16:13:04 -0700176 TombstoneSection(int id, const char* type, int64_t timeoutMs = 30000 /* 30 seconds */);
Kweku Adamseadd1232018-02-05 16:45:13 -0800177 virtual ~TombstoneSection();
178
179 virtual status_t BlockingCall(int pipeWriteFd) const;
180
181private:
182 std::string mType;
183};
184
Yi Jin6cacbcb2018-03-30 14:04:52 -0700185} // namespace incidentd
186} // namespace os
187} // namespace android
188
Yi Jinb592e3b2018-02-01 15:17:04 -0800189#endif // SECTIONS_H