| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 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 |  | 
 | 17 | #ifndef SECTIONS_H | 
 | 18 | #define SECTIONS_H | 
 | 19 |  | 
| Yi Jin | 99c248f | 2017-08-25 18:11:58 -0700 | [diff] [blame] | 20 | #include "Reporter.h" | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 21 |  | 
| Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 22 | #include <map> | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 23 | #include <stdarg.h> | 
| Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 24 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 25 | #include <utils/String8.h> | 
 | 26 | #include <utils/String16.h> | 
 | 27 | #include <utils/Vector.h> | 
 | 28 |  | 
 | 29 | using namespace android; | 
 | 30 |  | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 31 | const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds | 
 | 32 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 33 | /** | 
 | 34 |  * Base class for sections | 
 | 35 |  */ | 
 | 36 | class Section | 
 | 37 | { | 
 | 38 | public: | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 39 |     const int id; | 
 | 40 |     const int64_t timeoutMs; // each section must have a timeout | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 41 |     String8 name; | 
 | 42 |  | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 43 |     Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 44 |     virtual ~Section(); | 
 | 45 |  | 
 | 46 |     virtual status_t Execute(ReportRequestSet* requests) const = 0; | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 47 | }; | 
 | 48 |  | 
 | 49 | /** | 
| Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 50 |  * Section that generates incident headers. | 
 | 51 |  */ | 
 | 52 | class HeaderSection : public Section | 
 | 53 | { | 
 | 54 | public: | 
 | 55 |     HeaderSection(); | 
 | 56 |     virtual ~HeaderSection(); | 
 | 57 |  | 
 | 58 |     virtual status_t Execute(ReportRequestSet* requests) const; | 
 | 59 | }; | 
 | 60 |  | 
 | 61 | /** | 
| Yi Jin | 329130b | 2018-02-09 16:47:47 -0800 | [diff] [blame^] | 62 |  * Section that generates incident metadata. | 
 | 63 |  */ | 
 | 64 | class MetadataSection : public Section | 
 | 65 | { | 
 | 66 | public: | 
 | 67 |     MetadataSection(); | 
 | 68 |     virtual ~MetadataSection(); | 
 | 69 |  | 
 | 70 |     virtual status_t Execute(ReportRequestSet* requests) const; | 
 | 71 | }; | 
 | 72 |  | 
 | 73 | /** | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 74 |  * Section that reads in a file. | 
 | 75 |  */ | 
 | 76 | class FileSection : public Section | 
 | 77 | { | 
 | 78 | public: | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 79 |     FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 80 |     virtual ~FileSection(); | 
 | 81 |  | 
 | 82 |     virtual status_t Execute(ReportRequestSet* requests) const; | 
 | 83 |  | 
 | 84 | private: | 
 | 85 |     const char* mFilename; | 
| Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 86 |     bool mIsSysfs; // sysfs files are pollable but return POLLERR by default, handle it separately | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 87 | }; | 
 | 88 |  | 
 | 89 | /** | 
 | 90 |  * Base class for sections that call a command that might need a timeout. | 
 | 91 |  */ | 
 | 92 | class WorkerThreadSection : public Section | 
 | 93 | { | 
 | 94 | public: | 
 | 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 |  */ | 
 | 106 | class CommandSection : public Section | 
 | 107 | { | 
 | 108 | public: | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 109 |     CommandSection(int id, const int64_t timeoutMs, const char* command, ...); | 
 | 110 |  | 
 | 111 |     CommandSection(int id, const char* command, ...); | 
 | 112 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 113 |     virtual ~CommandSection(); | 
 | 114 |  | 
 | 115 |     virtual status_t Execute(ReportRequestSet* requests) const; | 
 | 116 |  | 
 | 117 | private: | 
 | 118 |     const char** mCommand; | 
| Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 119 |  | 
 | 120 |     void init(const char* command, va_list args); | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 121 | }; | 
 | 122 |  | 
 | 123 | /** | 
 | 124 |  * Section that calls dumpsys on a system service. | 
 | 125 |  */ | 
 | 126 | class DumpsysSection : public WorkerThreadSection | 
 | 127 | { | 
 | 128 | public: | 
 | 129 |     DumpsysSection(int id, const char* service, ...); | 
 | 130 |     virtual ~DumpsysSection(); | 
 | 131 |  | 
 | 132 |     virtual status_t BlockingCall(int pipeWriteFd) const; | 
 | 133 |  | 
 | 134 | private: | 
 | 135 |     String16 mService; | 
 | 136 |     Vector<String16> mArgs; | 
 | 137 | }; | 
 | 138 |  | 
| Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 139 | /** | 
 | 140 |  * Section that reads from logd. | 
 | 141 |  */ | 
 | 142 | class 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 |  | 
 | 147 | public: | 
 | 148 |     LogSection(int id, log_id_t logID); | 
 | 149 |     virtual ~LogSection(); | 
 | 150 |  | 
 | 151 |     virtual status_t BlockingCall(int pipeWriteFd) const; | 
 | 152 |  | 
 | 153 | private: | 
 | 154 |     log_id_t mLogID; | 
 | 155 |     bool mBinary; | 
 | 156 | }; | 
 | 157 |  | 
| Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 158 | #endif // SECTIONS_H | 
 | 159 |  |