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 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 62 | * Section that reads in a file. |
| 63 | */ |
| 64 | class FileSection : public Section |
| 65 | { |
| 66 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 67 | 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] | 68 | virtual ~FileSection(); |
| 69 | |
| 70 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 71 | |
| 72 | private: |
| 73 | const char* mFilename; |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 74 | 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] | 75 | }; |
| 76 | |
| 77 | /** |
| 78 | * Base class for sections that call a command that might need a timeout. |
| 79 | */ |
| 80 | class WorkerThreadSection : public Section |
| 81 | { |
| 82 | public: |
| 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 | */ |
| 94 | class CommandSection : public Section |
| 95 | { |
| 96 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 97 | CommandSection(int id, const int64_t timeoutMs, const char* command, ...); |
| 98 | |
| 99 | CommandSection(int id, const char* command, ...); |
| 100 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 101 | virtual ~CommandSection(); |
| 102 | |
| 103 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 104 | |
| 105 | private: |
| 106 | const char** mCommand; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 107 | |
| 108 | void init(const char* command, va_list args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 109 | }; |
| 110 | |
| 111 | /** |
| 112 | * Section that calls dumpsys on a system service. |
| 113 | */ |
| 114 | class DumpsysSection : public WorkerThreadSection |
| 115 | { |
| 116 | public: |
| 117 | DumpsysSection(int id, const char* service, ...); |
| 118 | virtual ~DumpsysSection(); |
| 119 | |
| 120 | virtual status_t BlockingCall(int pipeWriteFd) const; |
| 121 | |
| 122 | private: |
| 123 | String16 mService; |
| 124 | Vector<String16> mArgs; |
| 125 | }; |
| 126 | |
Yi Jin | 3c034c9 | 2017-12-22 17:36:47 -0800 | [diff] [blame] | 127 | /** |
| 128 | * Section that reads from logd. |
| 129 | */ |
| 130 | class 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 | |
| 135 | public: |
| 136 | LogSection(int id, log_id_t logID); |
| 137 | virtual ~LogSection(); |
| 138 | |
| 139 | virtual status_t BlockingCall(int pipeWriteFd) const; |
| 140 | |
| 141 | private: |
| 142 | log_id_t mLogID; |
| 143 | bool mBinary; |
| 144 | }; |
| 145 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 146 | #endif // SECTIONS_H |
| 147 | |