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 | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 22 | #include <stdarg.h> |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 23 | #include <utils/String8.h> |
| 24 | #include <utils/String16.h> |
| 25 | #include <utils/Vector.h> |
| 26 | |
| 27 | using namespace android; |
| 28 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 29 | const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds |
| 30 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 31 | /** |
| 32 | * Base class for sections |
| 33 | */ |
| 34 | class Section |
| 35 | { |
| 36 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 37 | const int id; |
| 38 | const int64_t timeoutMs; // each section must have a timeout |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 39 | String8 name; |
| 40 | |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 41 | Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 42 | virtual ~Section(); |
| 43 | |
| 44 | virtual status_t Execute(ReportRequestSet* requests) const = 0; |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 45 | }; |
| 46 | |
| 47 | /** |
Yi Jin | edfd5bb | 2017-09-06 17:09:11 -0700 | [diff] [blame] | 48 | * Section that generates incident headers. |
| 49 | */ |
| 50 | class HeaderSection : public Section |
| 51 | { |
| 52 | public: |
| 53 | HeaderSection(); |
| 54 | virtual ~HeaderSection(); |
| 55 | |
| 56 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 57 | }; |
| 58 | |
| 59 | /** |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 60 | * Section that reads in a file. |
| 61 | */ |
| 62 | class FileSection : public Section |
| 63 | { |
| 64 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 65 | 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] | 66 | virtual ~FileSection(); |
| 67 | |
| 68 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 69 | |
| 70 | private: |
| 71 | const char* mFilename; |
Yi Jin | 0eb2234 | 2017-11-06 17:17:27 -0800 | [diff] [blame] | 72 | 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] | 73 | }; |
| 74 | |
| 75 | /** |
| 76 | * Base class for sections that call a command that might need a timeout. |
| 77 | */ |
| 78 | class WorkerThreadSection : public Section |
| 79 | { |
| 80 | public: |
| 81 | WorkerThreadSection(int id); |
| 82 | virtual ~WorkerThreadSection(); |
| 83 | |
| 84 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 85 | |
| 86 | virtual status_t BlockingCall(int pipeWriteFd) const = 0; |
| 87 | }; |
| 88 | |
| 89 | /** |
| 90 | * Section that forks and execs a command, and puts stdout as the section. |
| 91 | */ |
| 92 | class CommandSection : public Section |
| 93 | { |
| 94 | public: |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 95 | CommandSection(int id, const int64_t timeoutMs, const char* command, ...); |
| 96 | |
| 97 | CommandSection(int id, const char* command, ...); |
| 98 | |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 99 | virtual ~CommandSection(); |
| 100 | |
| 101 | virtual status_t Execute(ReportRequestSet* requests) const; |
| 102 | |
| 103 | private: |
| 104 | const char** mCommand; |
Yi Jin | b44f7d4 | 2017-07-21 12:12:59 -0700 | [diff] [blame] | 105 | |
| 106 | void init(const char* command, va_list args); |
Joe Onorato | 1754d74 | 2016-11-21 17:51:35 -0800 | [diff] [blame] | 107 | }; |
| 108 | |
| 109 | /** |
| 110 | * Section that calls dumpsys on a system service. |
| 111 | */ |
| 112 | class DumpsysSection : public WorkerThreadSection |
| 113 | { |
| 114 | public: |
| 115 | DumpsysSection(int id, const char* service, ...); |
| 116 | virtual ~DumpsysSection(); |
| 117 | |
| 118 | virtual status_t BlockingCall(int pipeWriteFd) const; |
| 119 | |
| 120 | private: |
| 121 | String16 mService; |
| 122 | Vector<String16> mArgs; |
| 123 | }; |
| 124 | |
| 125 | #endif // SECTIONS_H |
| 126 | |