blob: 93b4848f5bd8bbc3313a1413bd870ee2631d91bd [file] [log] [blame]
Joe Onorato1754d742016-11-21 17:51:35 -08001/*
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
20#include "FdBuffer.h"
21
Yi Jinb44f7d42017-07-21 12:12:59 -070022#include <stdarg.h>
Joe Onorato1754d742016-11-21 17:51:35 -080023#include <utils/String8.h>
24#include <utils/String16.h>
25#include <utils/Vector.h>
26
27using namespace android;
28
Yi Jinb44f7d42017-07-21 12:12:59 -070029const int64_t REMOTE_CALL_TIMEOUT_MS = 10 * 1000; // 10 seconds
30
Joe Onorato1754d742016-11-21 17:51:35 -080031/**
32 * Base class for sections
33 */
34class Section
35{
36public:
Yi Jinb44f7d42017-07-21 12:12:59 -070037 const int id;
38 const int64_t timeoutMs; // each section must have a timeout
Joe Onorato1754d742016-11-21 17:51:35 -080039 String8 name;
40
Yi Jinb44f7d42017-07-21 12:12:59 -070041 Section(int id, const int64_t timeoutMs = REMOTE_CALL_TIMEOUT_MS);
Joe Onorato1754d742016-11-21 17:51:35 -080042 virtual ~Section();
43
44 virtual status_t Execute(ReportRequestSet* requests) const = 0;
45
46 status_t WriteHeader(ReportRequestSet* requests, size_t size) const;
47};
48
49/**
50 * Section that reads in a file.
51 */
52class FileSection : public Section
53{
54public:
Yi Jinb44f7d42017-07-21 12:12:59 -070055 FileSection(int id, const char* filename, const int64_t timeoutMs = 5000 /* 5 seconds */);
Joe Onorato1754d742016-11-21 17:51:35 -080056 virtual ~FileSection();
57
58 virtual status_t Execute(ReportRequestSet* requests) const;
59
60private:
61 const char* mFilename;
62};
63
64/**
65 * Base class for sections that call a command that might need a timeout.
66 */
67class WorkerThreadSection : public Section
68{
69public:
70 WorkerThreadSection(int id);
71 virtual ~WorkerThreadSection();
72
73 virtual status_t Execute(ReportRequestSet* requests) const;
74
75 virtual status_t BlockingCall(int pipeWriteFd) const = 0;
76};
77
78/**
79 * Section that forks and execs a command, and puts stdout as the section.
80 */
81class CommandSection : public Section
82{
83public:
Yi Jinb44f7d42017-07-21 12:12:59 -070084 CommandSection(int id, const int64_t timeoutMs, const char* command, ...);
85
86 CommandSection(int id, const char* command, ...);
87
Joe Onorato1754d742016-11-21 17:51:35 -080088 virtual ~CommandSection();
89
90 virtual status_t Execute(ReportRequestSet* requests) const;
91
92private:
93 const char** mCommand;
Yi Jinb44f7d42017-07-21 12:12:59 -070094
95 void init(const char* command, va_list args);
Joe Onorato1754d742016-11-21 17:51:35 -080096};
97
98/**
99 * Section that calls dumpsys on a system service.
100 */
101class DumpsysSection : public WorkerThreadSection
102{
103public:
104 DumpsysSection(int id, const char* service, ...);
105 virtual ~DumpsysSection();
106
107 virtual status_t BlockingCall(int pipeWriteFd) const;
108
109private:
110 String16 mService;
111 Vector<String16> mArgs;
112};
113
114#endif // SECTIONS_H
115