1 /*
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 #pragma once
17 
18 #ifndef SECTIONS_H
19 #define SECTIONS_H
20 
21 #include "Reporter.h"
22 
23 #include <stdarg.h>
24 #include <map>
25 
26 #include <utils/String16.h>
27 #include <utils/String8.h>
28 #include <utils/Vector.h>
29 
30 namespace android {
31 namespace os {
32 namespace incidentd {
33 
34 const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000;  // 30 seconds
35 
36 /**
37  * Base class for sections
38  */
39 class Section {
40 public:
41     const int id;
42     const int64_t timeoutMs;  // each section must have a timeout
43     const bool userdebugAndEngOnly;
44     const bool deviceSpecific;
45     String8 name;
46 
47     Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS, bool userdebugAndEngOnly = false,
48             bool deviceSpecific = false);
49     virtual ~Section();
50 
51     virtual status_t Execute(ReportRequestSet* requests) const = 0;
52 };
53 
54 /**
55  * Section that generates incident headers.
56  */
57 class HeaderSection : public Section {
58 public:
59     HeaderSection();
60     virtual ~HeaderSection();
61 
62     virtual status_t Execute(ReportRequestSet* requests) const;
63 };
64 
65 /**
66  * Section that generates incident metadata.
67  */
68 class MetadataSection : public Section {
69 public:
70     MetadataSection();
71     virtual ~MetadataSection();
72 
73     virtual status_t Execute(ReportRequestSet* requests) const;
74 };
75 
76 /**
77  * Section that reads in a file.
78  */
79 class FileSection : public Section {
80 public:
81     FileSection(int id, const char* filename, bool deviceSpecific = false,
82                 int64_t timeoutMs = 5000 /* 5 seconds */);
83     virtual ~FileSection();
84 
85     virtual status_t Execute(ReportRequestSet* requests) const;
86 
87 private:
88     const char* mFilename;
89     bool mIsSysfs;  // sysfs files are pollable but return POLLERR by default, handle it separately
90 };
91 
92 /**
93  * Section that reads in a file and gzips the content.
94  */
95 class GZipSection : public Section {
96 public:
97     GZipSection(int id, const char* filename, ...);
98     virtual ~GZipSection();
99 
100     virtual status_t Execute(ReportRequestSet* requests) const;
101 
102 private:
103     // It looks up the content from multiple files and stops when the first one is available.
104     const char** mFilenames;
105 };
106 
107 /**
108  * Base class for sections that call a command that might need a timeout.
109  */
110 class WorkerThreadSection : public Section {
111 public:
112     WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS,
113                         bool userdebugAndEngOnly = false);
114     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  */
124 class CommandSection : public Section {
125 public:
126     CommandSection(int id, int64_t timeoutMs, const char* command, ...);
127 
128     CommandSection(int id, const char* command, ...);
129 
130     virtual ~CommandSection();
131 
132     virtual status_t Execute(ReportRequestSet* requests) const;
133 
134 private:
135     const char** mCommand;
136 };
137 
138 /**
139  * Section that calls dumpsys on a system service.
140  */
141 class DumpsysSection : public WorkerThreadSection {
142 public:
143     DumpsysSection(int id, bool userdebugAndEngOnly, const char* service, ...);
144     virtual ~DumpsysSection();
145 
146     virtual status_t BlockingCall(int pipeWriteFd) const;
147 
148 private:
149     String16 mService;
150     Vector<String16> mArgs;
151 };
152 
153 /**
154  * Section that reads from logd.
155  */
156 class LogSection : public WorkerThreadSection {
157     // global last log retrieved timestamp for each log_id_t.
158     static map<log_id_t, log_time> gLastLogsRetrieved;
159 
160 public:
161     LogSection(int id, log_id_t logID);
162     virtual ~LogSection();
163 
164     virtual status_t BlockingCall(int pipeWriteFd) const;
165 
166 private:
167     log_id_t mLogID;
168     bool mBinary;
169 };
170 
171 /**
172  * Section that gets data from tombstoned.
173  */
174 class TombstoneSection : public WorkerThreadSection {
175 public:
176     TombstoneSection(int id, const char* type, int64_t timeoutMs = 30000 /* 30 seconds */);
177     virtual ~TombstoneSection();
178 
179     virtual status_t BlockingCall(int pipeWriteFd) const;
180 
181 private:
182     std::string mType;
183 };
184 
185 }  // namespace incidentd
186 }  // namespace os
187 }  // namespace android
188 
189 #endif  // SECTIONS_H
190