blob: d440ee92601ca650123e39bdf86ede16bd40a485 [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/**
Joe Onorato1754d742016-11-21 17:51:35 -080062 * Section that reads in a file.
63 */
64class FileSection : public Section
65{
66public:
Yi Jinb44f7d42017-07-21 12:12:59 -070067 FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080068 virtual ~FileSection();
69
70 virtual status_t Execute(ReportRequestSet* requests) const;
71
72private:
73 const char* mFilename;
Yi Jin0eb22342017-11-06 17:17:27 -080074 bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately
Joe Onorato1754d742016-11-21 17:51:35 -080075};
76
77/**
78 * Base class for sections that call a command that might need a timeout.
79 */
80class WorkerThreadSection : public Section
81{
82public:
83 WorkerThreadSection(int id);
84 virtual ~WorkerThreadSection();
85
86 virtual status_t Execute(ReportRequestSet* requests) const;
87
88 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
89};
90
91/**
92 * Section that forks and execs a command, and puts stdout as the section.
93 */
94class CommandSection : public Section
95{
96public:
Yi Jinb44f7d42017-07-21 12:12:59 -070097 CommandSection(int id, const int64_t timeoutMs, const char* command, ...);
98
99 CommandSection(int id, const char* command, ...);
100
Joe Onorato1754d742016-11-21 17:51:35 -0800101 virtual ~CommandSection();
102
103 virtual status_t Execute(ReportRequestSet* requests) const;
104
105private:
106 const char** mCommand;
Yi Jinb44f7d42017-07-21 12:12:59 -0700107
108 void init(const char* command, va_list args);
Joe Onorato1754d742016-11-21 17:51:35 -0800109};
110
111/**
112 * Section that calls dumpsys on a system service.
113 */
114class DumpsysSection : public WorkerThreadSection
115{
116public:
117 DumpsysSection(int id, const char* service, ...);
118 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/**
128 * Section that reads from logd.
129 */
130class LogSection : public WorkerThreadSection
131{
132 // global last log retrieved timestamp for each log_id_t.
133 static map<log_id_t, log_time> gLastLogsRetrieved;
134
135public:
136 LogSection(int id, log_id_t logID);
137 virtual ~LogSection();
138
139 virtual status_t BlockingCall(int pipeWriteFd) const;
140
141private:
142 log_id_t mLogID;
143 bool mBinary;
144};
145
Joe Onorato1754d742016-11-21 17:51:35 -0800146#endif // SECTIONS_H
147