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 <android/os/IIncidentDumpCallback.h>
27 
28 #include <utils/String16.h>
29 #include <utils/String8.h>
30 #include <utils/Vector.h>
31 
32 namespace android {
33 namespace os {
34 namespace incidentd {
35 
36 const int64_t REMOTE_CALL_TIMEOUT_MS = 30 * 1000;  // 30 seconds
37 
38 /**
39  * Base class for sections
40  */
41 class Section {
42 public:
43     const int id;
44     const int64_t timeoutMs;  // each section must have a timeout
45     String8 name;
46 
47     Section(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
48     virtual ~Section();
49 
50     virtual status_t Execute(ReportWriter* writer) const = 0;
51 };
52 
53 /**
54  * Section that reads in a file.
55  */
56 class FileSection : public Section {
57 public:
58     FileSection(int id, const char* filename,
59                 int64_t timeoutMs = 5000 /* 5 seconds */);
60     virtual ~FileSection();
61 
62     virtual status_t Execute(ReportWriter* writer) const;
63 
64 private:
65     const char* mFilename;
66     bool mIsSysfs;  // sysfs files are pollable but return POLLERR by default, handle it separately
67 };
68 
69 /**
70  * Section that reads in a file and gzips the content.
71  */
72 class GZipSection : public Section {
73 public:
74     GZipSection(int id, const char* filename, ...);
75     virtual ~GZipSection();
76 
77     virtual status_t Execute(ReportWriter* writer) const;
78 
79 private:
80     // It looks up the content from multiple files and stops when the first one is available.
81     const char** mFilenames;
82 };
83 
84 /**
85  * Base class for sections that call a command that might need a timeout.
86  */
87 class WorkerThreadSection : public Section {
88 public:
89     WorkerThreadSection(int id, int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
90     virtual ~WorkerThreadSection();
91 
92     virtual status_t Execute(ReportWriter* writer) const;
93 
94     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const = 0;
95 };
96 
97 /**
98  * Section that forks and execs a command, and puts stdout as the section.
99  */
100 class CommandSection : public Section {
101 public:
102     CommandSection(int id, int64_t timeoutMs, const char* command, ...);
103 
104     CommandSection(int id, const char* command, ...);
105 
106     virtual ~CommandSection();
107 
108     virtual status_t Execute(ReportWriter* writer) const;
109 
110 private:
111     const char** mCommand;
112 };
113 
114 /**
115  * Section that calls protobuf dumpsys on a system service, usually
116  * "dumpsys [service_name] --proto".
117  */
118 class DumpsysSection : public WorkerThreadSection {
119 public:
120     DumpsysSection(int id, const char* service, ...);
121     virtual ~DumpsysSection();
122 
123     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
124 
125 private:
126     String16 mService;
127     Vector<String16> mArgs;
128 };
129 
130 /**
131  * Section that calls text dumpsys on a system service, usually "dumpsys [service_name]".
132  */
133 class TextDumpsysSection : public Section {
134 public:
135     TextDumpsysSection(int id, const char* service, ...);
136     virtual ~TextDumpsysSection();
137 
138     virtual status_t Execute(ReportWriter* writer) const;
139 
140 private:
141     String16 mService;
142     Vector<String16> mArgs;
143 };
144 
145 /**
146  * Section that calls dumpsys on a system service.
147  */
148 class SystemPropertyDumpsysSection : public WorkerThreadSection {
149 public:
150     SystemPropertyDumpsysSection(int id, const char* service, ...);
151     virtual ~SystemPropertyDumpsysSection();
152 
153     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
154 
155 private:
156     String16 mService;
157     Vector<String16> mArgs;
158 };
159 
160 /**
161  * Section that reads from logd.
162  */
163 class LogSection : public WorkerThreadSection {
164     // global last log retrieved timestamp for each log_id_t.
165     static map<log_id_t, log_time> gLastLogsRetrieved;
166 
167     // log mode: read only & non blocking.
168     const static int logModeBase = ANDROID_LOG_RDONLY | ANDROID_LOG_NONBLOCK;
169 
170 public:
171     LogSection(int id, const char* logID, ...);
172     virtual ~LogSection();
173 
174     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
175 
176 private:
177     log_id_t mLogID;
178     bool mBinary;
179     int mLogMode;
180 };
181 
182 /**
183  * Section that gets data from tombstoned.
184  */
185 class TombstoneSection : public WorkerThreadSection {
186 public:
187     TombstoneSection(int id, const char* type, int64_t timeoutMs = 120000 /* 2 minutes */);
188     virtual ~TombstoneSection();
189 
190     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
191 
192 private:
193     std::string mType;
194 };
195 
196 /**
197  * Section that gets data from a registered dump callback.
198  */
199 class BringYourOwnSection : public WorkerThreadSection {
200 public:
201     const uid_t uid;
202 
203     BringYourOwnSection(int id, const char* customName, const uid_t callingUid,
204         const sp<IIncidentDumpCallback>& callback);
205     virtual ~BringYourOwnSection();
206 
207     virtual status_t BlockingCall(unique_fd& pipeWriteFd) const;
208 
209 private:
210     const sp<IIncidentDumpCallback> mCallback;
211 };
212 
213 
214 /**
215  * These sections will not be generated when doing an 'all' report, either
216  * for size, speed of collection, or privacy.
217  */
218 bool section_requires_specific_mention(int sectionId);
219 
220 }  // namespace incidentd
221 }  // namespace os
222 }  // namespace android
223 
224 #endif  // SECTIONS_H
225