blob: cfe7e1648ad80cf3e68585eb03b52baf9a6fd533 [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
Joe Onorato1754d742016-11-21 17:51:35 -080043 String8 name;
44
Joe Onoratofe7bbf42019-03-24 20:57:16 -070045 Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080046 virtual ~Section();
47
Joe Onorato99598ee2019-02-11 15:55:13 +000048 virtual status_t Execute(ReportWriter* writer) const = 0;
Yi Jin329130b2018-02-09 16:47:47 -080049};
50
51/**
Joe Onorato1754d742016-11-21 17:51:35 -080052 * Section that reads in a file.
53 */
Yi Jinb592e3b2018-02-01 15:17:04 -080054class FileSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080055public:
Kweku Adamse04ef772018-06-13 12:24:38 -070056 FileSection(int id, const char* filename,
Yi Jin3f360352018-04-16 16:13:04 -070057 int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080058 virtual ~FileSection();
59
Joe Onorato99598ee2019-02-11 15:55:13 +000060 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -080061
62private:
63 const char* mFilename;
Yi Jinb592e3b2018-02-01 15:17:04 -080064 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080065};
66
67/**
Yi Jin1a11fa12018-02-22 16:44:10 -080068 * Section that reads in a file and gzips the content.
69 */
70class GZipSection : public Section {
71public:
72 GZipSection(int id, const char* filename, ...);
73 virtual ~GZipSection();
74
Joe Onorato99598ee2019-02-11 15:55:13 +000075 virtual status_t Execute(ReportWriter* writer) const;
Yi Jin1a11fa12018-02-22 16:44:10 -080076
77private:
78 // It looks up the content from multiple files and stops when the first one is available.
79 const char** mFilenames;
80};
81
82/**
Joe Onorato1754d742016-11-21 17:51:35 -080083 * Base class for sections that call a command that might need a timeout.
84 */
Yi Jinb592e3b2018-02-01 15:17:04 -080085class WorkerThreadSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080086public:
Joe Onoratofe7bbf42019-03-24 20:57:16 -070087 WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080088 virtual ~WorkerThreadSection();
89
Joe Onorato99598ee2019-02-11 15:55:13 +000090 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -080091
92 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
93};
94
95/**
96 * Section that forks and execs a command, and puts stdout as the section.
97 */
Yi Jinb592e3b2018-02-01 15:17:04 -080098class CommandSection : public Section {
Joe Onorato1754d742016-11-21 17:51:35 -080099public:
Yi Jin3f360352018-04-16 16:13:04 -0700100 CommandSection(int id, int64_t timeoutMs, const char* command, ...);
Yi Jinb44f7d42017-07-21 12:12:59 -0700101
102 CommandSection(int id, const char* command, ...);
103
Joe Onorato1754d742016-11-21 17:51:35 -0800104 virtual ~CommandSection();
105
Joe Onorato99598ee2019-02-11 15:55:13 +0000106 virtual status_t Execute(ReportWriter* writer) const;
Joe Onorato1754d742016-11-21 17:51:35 -0800107
108private:
109 const char** mCommand;
Joe Onorato1754d742016-11-21 17:51:35 -0800110};
111
112/**
113 * Section that calls dumpsys on a system service.
114 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800115class DumpsysSection : public WorkerThreadSection {
Joe Onorato1754d742016-11-21 17:51:35 -0800116public:
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700117 DumpsysSection(int id, const char* service, ...);
Joe Onorato1754d742016-11-21 17:51:35 -0800118 virtual ~DumpsysSection();
119
120 virtual status_t BlockingCall(int pipeWriteFd) const;
121
122private:
123 String16 mService;
124 Vector<String16> mArgs;
125};
126
Yi Jin3c034c92017-12-22 17:36:47 -0800127/**
Joe Onoratofe7bbf42019-03-24 20:57:16 -0700128 * Section that calls dumpsys on a system service.
129 */
130class SystemPropertyDumpsysSection : public WorkerThreadSection {
131public:
132 SystemPropertyDumpsysSection(int id, const char* service, ...);
133 virtual ~SystemPropertyDumpsysSection();
134
135 virtual status_t BlockingCall(int pipeWriteFd) const;
136
137private:
138 String16 mService;
139 Vector<String16> mArgs;
140};
141
142/**
Yi Jin3c034c92017-12-22 17:36:47 -0800143 * Section that reads from logd.
144 */
Yi Jinb592e3b2018-02-01 15:17:04 -0800145class LogSection : public WorkerThreadSection {
Yi Jin3c034c92017-12-22 17:36:47 -0800146 // global last log retrieved timestamp for each log_id_t.
147 static map<log_id_t, log_time> gLastLogsRetrieved;
148
149public:
150 LogSection(int id, log_id_t logID);
151 virtual ~LogSection();
152
153 virtual status_t BlockingCall(int pipeWriteFd) const;
154
155private:
156 log_id_t mLogID;
157 bool mBinary;
158};
159
Kweku Adamseadd1232018-02-05 16:45:13 -0800160/**
161 * Section that gets data from tombstoned.
162 */
163class TombstoneSection : public WorkerThreadSection {
164public:
Kweku Adams0f716792018-09-13 15:48:27 -0700165 TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */);
Kweku Adamseadd1232018-02-05 16:45:13 -0800166 virtual ~TombstoneSection();
167
168 virtual status_t BlockingCall(int pipeWriteFd) const;
169
170private:
171 std::string mType;
172};
173
Yi Jin6cacbcb2018-03-30 14:04:52 -0700174} // namespace incidentd
175} // namespace os
176} // namespace android
177
Yi Jinb592e3b2018-02-01 15:17:04 -0800178#endif // SECTIONS_H