blob: 80cc033d5ca065f402196728300bbabef89398d1 [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 */
16
17#ifndef SECTIONS_H
18#define SECTIONS_H
19
Yi Jin99c248f2017-08-25 18:11:58 -070020#include "Reporter.h"
Joe Onorato1754d742016-11-21 17:51:35 -080021
Yi Jin3c034c92017-12-22 17:36:47 -080022#include <map>
Yi Jinb44f7d42017-07-21 12:12:59 -070023#include <stdarg.h>
Yi Jin3c034c92017-12-22 17:36:47 -080024
Joe Onorato1754d742016-11-21 17:51:35 -080025#include <utils/String8.h>
26#include <utils/String16.h>
27#include <utils/Vector.h>
28
29using namespace android;
30
Yi Jinb44f7d42017-07-21 12:12:59 -070031const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds
32
Joe Onorato1754d742016-11-21 17:51:35 -080033/**
34 * Base class for sections
35 */
36class Section
37{
38public:
Yi Jinb44f7d42017-07-21 12:12:59 -070039 const int id;
40 const int64_t timeoutMs; // each section must have a timeout
Joe Onorato1754d742016-11-21 17:51:35 -080041 String8 name;
42
Yi Jinb44f7d42017-07-21 12:12:59 -070043 Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080044 virtual ~Section();
45
46 virtual status_t Execute(ReportRequestSet* requests) const = 0;
Joe Onorato1754d742016-11-21 17:51:35 -080047};
48
49/**
Yi Jinedfd5bb2017-09-06 17:09:11 -070050 * Section that generates incident headers.
51 */
52class HeaderSection : public Section
53{
54public:
55 HeaderSection();
56 virtual ~HeaderSection();
57
58 virtual status_t Execute(ReportRequestSet* requests) const;
59};
60
61/**
Yi Jin329130b2018-02-09 16:47:47 -080062 * Section that generates incident metadata.
63 */
64class MetadataSection : public Section
65{
66public:
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 */
76class FileSection : public Section
77{
78public:
Yi Jinb44f7d42017-07-21 12:12:59 -070079 FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080080 virtual ~FileSection();
81
82 virtual status_t Execute(ReportRequestSet* requests) const;
83
84private:
85 const char* mFilename;
Yi Jin0eb22342017-11-06 17:17:27 -080086 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080087};
88
89/**
90 * Base class for sections that call a command that might need a timeout.
91 */
92class WorkerThreadSection : public Section
93{
94public:
95 WorkerThreadSection(int id);
96 virtual ~WorkerThreadSection();
97
98 virtual status_t Execute(ReportRequestSet* requests) const;
99
100 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
101};
102
103/**
104 * Section that forks and execs a command, and puts stdout as the section.
105 */
106class CommandSection : public Section
107{
108public:
Yi Jinb44f7d42017-07-21 12:12:59 -0700109 CommandSection(int id, const int64_t timeoutMs, const char* command, ...);
110
111 CommandSection(int id, const char* command, ...);
112
Joe Onorato1754d742016-11-21 17:51:35 -0800113 virtual ~CommandSection();
114
115 virtual status_t Execute(ReportRequestSet* requests) const;
116
117private:
118 const char** mCommand;
Yi Jinb44f7d42017-07-21 12:12:59 -0700119
120 void init(const char* command, va_list args);
Joe Onorato1754d742016-11-21 17:51:35 -0800121};
122
123/**
124 * Section that calls dumpsys on a system service.
125 */
126class DumpsysSection : public WorkerThreadSection
127{
128public:
129 DumpsysSection(int id, const char* service, ...);
130 virtual ~DumpsysSection();
131
132 virtual status_t BlockingCall(int pipeWriteFd) const;
133
134private:
135 String16 mService;
136 Vector<String16> mArgs;
137};
138
Yi Jin3c034c92017-12-22 17:36:47 -0800139/**
140 * Section that reads from logd.
141 */
142class LogSection : public WorkerThreadSection
143{
144 // global last log retrieved timestamp for each log_id_t.
145 static map<log_id_t, log_time> gLastLogsRetrieved;
146
147public:
148 LogSection(int id, log_id_t logID);
149 virtual ~LogSection();
150
151 virtual status_t BlockingCall(int pipeWriteFd) const;
152
153private:
154 log_id_t mLogID;
155 bool mBinary;
156};
157
Joe Onorato1754d742016-11-21 17:51:35 -0800158#endif // SECTIONS_H
159