1 | /* $Id: VBoxWatchdogInternal.h 106061 2024-09-16 14:03:52Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxWatchdog - VirtualBox Watchdog Service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2024 Oracle and/or its affiliates.
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox base platform packages, as
|
---|
10 | * available from https://www.virtualbox.org.
|
---|
11 | *
|
---|
12 | * This program is free software; you can redistribute it and/or
|
---|
13 | * modify it under the terms of the GNU General Public License
|
---|
14 | * as published by the Free Software Foundation, in version 3 of the
|
---|
15 | * License.
|
---|
16 | *
|
---|
17 | * This program is distributed in the hope that it will be useful, but
|
---|
18 | * WITHOUT ANY WARRANTY; without even the implied warranty of
|
---|
19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
---|
20 | * General Public License for more details.
|
---|
21 | *
|
---|
22 | * You should have received a copy of the GNU General Public License
|
---|
23 | * along with this program; if not, see <https://www.gnu.org/licenses>.
|
---|
24 | *
|
---|
25 | * SPDX-License-Identifier: GPL-3.0-only
|
---|
26 | */
|
---|
27 |
|
---|
28 | #ifndef VBOX_INCLUDED_SRC_VBoxBalloonCtrl_VBoxWatchdogInternal_h
|
---|
29 | #define VBOX_INCLUDED_SRC_VBoxBalloonCtrl_VBoxWatchdogInternal_h
|
---|
30 | #ifndef RT_WITHOUT_PRAGMA_ONCE
|
---|
31 | # pragma once
|
---|
32 | #endif
|
---|
33 |
|
---|
34 | #ifndef VBOX_ONLY_DOCS
|
---|
35 | # include <iprt/getopt.h>
|
---|
36 | # include <iprt/time.h>
|
---|
37 |
|
---|
38 | # include <VBox/err.h>
|
---|
39 | # include <VBox/com/com.h>
|
---|
40 | # include <VBox/com/string.h>
|
---|
41 | # include <VBox/com/Guid.h>
|
---|
42 | # include <VBox/com/array.h>
|
---|
43 | # include <VBox/com/ErrorInfo.h>
|
---|
44 | # include <VBox/com/VirtualBox.h>
|
---|
45 | #endif /* !VBOX_ONLY_DOCS */
|
---|
46 |
|
---|
47 | #include <map>
|
---|
48 | #include <vector>
|
---|
49 |
|
---|
50 | using namespace com;
|
---|
51 |
|
---|
52 | ////////////////////////////////////////////////////////////////////////////////
|
---|
53 | //
|
---|
54 | // definitions
|
---|
55 | //
|
---|
56 | ////////////////////////////////////////////////////////////////////////////////
|
---|
57 |
|
---|
58 | /** Command handler argument. */
|
---|
59 | struct HandlerArg
|
---|
60 | {
|
---|
61 | int argc;
|
---|
62 | char **argv;
|
---|
63 | };
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * A module's payload for a machine entry.
|
---|
67 | * The payload data is not (yet) thread safe -- so only
|
---|
68 | * use this in one module at a time only!
|
---|
69 | */
|
---|
70 | typedef struct VBOXWATCHDOG_MODULE_PAYLOAD
|
---|
71 | {
|
---|
72 | /** Pointer to allocated payload. Can be NULL if
|
---|
73 | * a module doesn't have an own payload. */
|
---|
74 | void *pvData;
|
---|
75 | /** Size of payload (in bytes). */
|
---|
76 | size_t cbData;
|
---|
77 | /** @todo Add mutex for locking + getPayloadLocked(). */
|
---|
78 | } VBOXWATCHDOG_MODULE_PAYLOAD, *PVBOXWATCHDOG_MODULE_PAYLOAD;
|
---|
79 |
|
---|
80 | /**
|
---|
81 | * Map containing a module's individual payload -- the module itself
|
---|
82 | * is responsible for allocating/handling/destroying this payload.
|
---|
83 | * Primary key is the module name.
|
---|
84 | */
|
---|
85 | typedef std::map<const char*, VBOXWATCHDOG_MODULE_PAYLOAD> mapPayload;
|
---|
86 | typedef std::map<const char*, VBOXWATCHDOG_MODULE_PAYLOAD>::iterator mapPayloadIter;
|
---|
87 | typedef std::map<const char*, VBOXWATCHDOG_MODULE_PAYLOAD>::const_iterator mapPayloadIterConst;
|
---|
88 |
|
---|
89 | /** Group list (plus additional per-group flags, not used yet) for one VM.
|
---|
90 | * Primary key is the group name, secondary specify flags (if any). */
|
---|
91 | typedef std::map<Utf8Str, uint32_t> mapGroups;
|
---|
92 | typedef std::map<Utf8Str, uint32_t>::iterator mapGroupsIter;
|
---|
93 | typedef std::map<Utf8Str, uint32_t>::const_iterator mapGroupsIterConst;
|
---|
94 |
|
---|
95 | /** A machine's internal entry.
|
---|
96 | * Primary key is the machine's UUID. */
|
---|
97 | typedef struct VBOXWATCHDOG_MACHINE
|
---|
98 | {
|
---|
99 | ComPtr<IMachine> machine;
|
---|
100 | /** The machine's name. For logging. */
|
---|
101 | Bstr strName;
|
---|
102 | #ifndef VBOX_WATCHDOG_GLOBAL_PERFCOL
|
---|
103 | ComPtr<IPerformanceCollector> collector;
|
---|
104 | #endif
|
---|
105 | /** The group(s) this machine belongs to. */
|
---|
106 | mapGroups groups;
|
---|
107 | /** Map containing the individual module payloads. */
|
---|
108 | mapPayload payload;
|
---|
109 | } VBOXWATCHDOG_MACHINE, *PVBOXWATCHDOG_MACHINE;
|
---|
110 | typedef std::map<Bstr, VBOXWATCHDOG_MACHINE> mapVM;
|
---|
111 | typedef std::map<Bstr, VBOXWATCHDOG_MACHINE>::iterator mapVMIter;
|
---|
112 | typedef std::map<Bstr, VBOXWATCHDOG_MACHINE>::const_iterator mapVMIterConst;
|
---|
113 |
|
---|
114 | /** Members of a VM group; currently only represented by the machine's UUID.
|
---|
115 | * Primary key is the machine's UUID. */
|
---|
116 | typedef std::vector<Bstr> vecGroupMembers;
|
---|
117 | typedef std::vector<Bstr>::iterator vecGroupMembersIter;
|
---|
118 | typedef std::vector<Bstr>::const_iterator vecGroupMembersIterConst;
|
---|
119 |
|
---|
120 | /** A VM group. Can contain none, one or more group members.
|
---|
121 | * Primary key is the group's name. */
|
---|
122 | typedef std::map<Utf8Str, vecGroupMembers> mapGroup;
|
---|
123 | typedef std::map<Utf8Str, vecGroupMembers>::iterator mapGroupIter;
|
---|
124 | typedef std::map<Utf8Str, vecGroupMembers>::const_iterator mapGroupIterConst;
|
---|
125 |
|
---|
126 | /**
|
---|
127 | * A module descriptor.
|
---|
128 | */
|
---|
129 | typedef struct
|
---|
130 | {
|
---|
131 | /** The short module name. */
|
---|
132 | const char *pszName;
|
---|
133 | /** The longer module name. */
|
---|
134 | const char *pszDescription;
|
---|
135 | /** A comma-separated list of modules this module
|
---|
136 | * depends on. Might be NULL if no dependencies. */
|
---|
137 | const char *pszDepends;
|
---|
138 | /** Priority (lower is higher, 0 is invalid) of
|
---|
139 | * module execution. */
|
---|
140 | uint32_t uPriority;
|
---|
141 | /** The usage options stuff for the --help screen. */
|
---|
142 | const char *pszUsage;
|
---|
143 | /** The option descriptions for the --help screen. */
|
---|
144 | const char *pszOptions;
|
---|
145 |
|
---|
146 | /**
|
---|
147 | * Called before parsing arguments.
|
---|
148 | * @returns VBox status code.
|
---|
149 | */
|
---|
150 | DECLCALLBACKMEMBER(int, pfnPreInit,(void));
|
---|
151 |
|
---|
152 | /**
|
---|
153 | * Tries to parse the given command line options.
|
---|
154 | *
|
---|
155 | * @returns 0 if we parsed, -1 if it didn't and anything else means exit.
|
---|
156 | * @param argc Argument count.
|
---|
157 | * @param argv Arguments.
|
---|
158 | * @param piConsumed How many parameters this callback consumed from the
|
---|
159 | * remaining arguments passed in.
|
---|
160 | */
|
---|
161 | DECLCALLBACKMEMBER(int, pfnOption,(int argc, char *argv[], int *piConsumed));
|
---|
162 |
|
---|
163 | /**
|
---|
164 | * Called before parsing arguments.
|
---|
165 | * @returns VBox status code.
|
---|
166 | */
|
---|
167 | DECLCALLBACKMEMBER(int, pfnInit,(void));
|
---|
168 |
|
---|
169 | /** Called from the watchdog's main function. Non-blocking.
|
---|
170 | *
|
---|
171 | * @returns VBox status code.
|
---|
172 | */
|
---|
173 | DECLCALLBACKMEMBER(int, pfnMain,(void));
|
---|
174 |
|
---|
175 | /**
|
---|
176 | * Stop the module.
|
---|
177 | */
|
---|
178 | DECLCALLBACKMEMBER(int, pfnStop,(void));
|
---|
179 |
|
---|
180 | /**
|
---|
181 | * Does termination cleanups.
|
---|
182 | *
|
---|
183 | * @remarks This may be called even if pfnInit hasn't been called!
|
---|
184 | */
|
---|
185 | DECLCALLBACKMEMBER(void, pfnTerm,(void));
|
---|
186 |
|
---|
187 | /** @name Callbacks.
|
---|
188 | * @{
|
---|
189 | */
|
---|
190 |
|
---|
191 | /**
|
---|
192 | *
|
---|
193 | * @returns VBox status code.
|
---|
194 | */
|
---|
195 | DECLCALLBACKMEMBER(int, pfnOnMachineRegistered,(const Bstr &strUuid));
|
---|
196 |
|
---|
197 | /**
|
---|
198 | *
|
---|
199 | * @returns VBox status code.
|
---|
200 | */
|
---|
201 | DECLCALLBACKMEMBER(int, pfnOnMachineUnregistered,(const Bstr &strUuid));
|
---|
202 |
|
---|
203 | /**
|
---|
204 | *
|
---|
205 | * @returns VBox status code.
|
---|
206 | */
|
---|
207 | DECLCALLBACKMEMBER(int, pfnOnMachineStateChanged,(const Bstr &strUuid, MachineState_T enmState));
|
---|
208 |
|
---|
209 | /**
|
---|
210 | *
|
---|
211 | * @returns VBox status code.
|
---|
212 | */
|
---|
213 | DECLCALLBACKMEMBER(int, pfnOnServiceStateChanged,(bool fAvailable));
|
---|
214 |
|
---|
215 | /** @} */
|
---|
216 | } VBOXMODULE;
|
---|
217 | /** Pointer to a VBOXMODULE. */
|
---|
218 | typedef VBOXMODULE *PVBOXMODULE;
|
---|
219 | /** Pointer to a const VBOXMODULE. */
|
---|
220 | typedef VBOXMODULE const *PCVBOXMODULE;
|
---|
221 |
|
---|
222 | RT_C_DECLS_BEGIN
|
---|
223 |
|
---|
224 | extern bool g_fDryrun;
|
---|
225 | extern bool g_fVerbose;
|
---|
226 | extern ComPtr<IVirtualBox> g_pVirtualBox;
|
---|
227 | extern ComPtr<ISession> g_pSession;
|
---|
228 | extern mapVM g_mapVM;
|
---|
229 | extern mapGroup g_mapGroup;
|
---|
230 | # ifdef VBOX_WATCHDOG_GLOBAL_PERFCOL
|
---|
231 | extern ComPtr<IPerformanceCollector> g_pPerfCollector;
|
---|
232 | # endif
|
---|
233 |
|
---|
234 | extern VBOXMODULE g_ModBallooning;
|
---|
235 | extern VBOXMODULE g_ModAPIMonitor;
|
---|
236 |
|
---|
237 | extern void serviceLog(const char *pszFormat, ...);
|
---|
238 | #define serviceLogVerbose(a) if (g_fVerbose) { serviceLog a; }
|
---|
239 |
|
---|
240 | int groupAdd(mapGroups &groups, const char *pszGroupsToAdd, uint32_t fFlags);
|
---|
241 |
|
---|
242 | extern int getMetric(PVBOXWATCHDOG_MACHINE pMachine, const Bstr& strName, LONG *pulData);
|
---|
243 | void* payloadFrom(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule);
|
---|
244 | int payloadAlloc(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule, size_t cbSize, void **ppszPayload);
|
---|
245 | void payloadFree(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule);
|
---|
246 |
|
---|
247 | PVBOXWATCHDOG_MACHINE getMachine(const Bstr& strUuid);
|
---|
248 | MachineState_T getMachineState(const PVBOXWATCHDOG_MACHINE pMachine);
|
---|
249 |
|
---|
250 | int cfgGetValueStr(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
|
---|
251 | const char *pszGlobal, const char *pszVM, Utf8Str &strValue, Utf8Str strDefault);
|
---|
252 | int cfgGetValueU32(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
|
---|
253 | const char *pszGlobal, const char *pszVM, uint32_t *puValue, uint32_t uDefault);
|
---|
254 | RT_C_DECLS_END
|
---|
255 |
|
---|
256 | #endif /* !VBOX_INCLUDED_SRC_VBoxBalloonCtrl_VBoxWatchdogInternal_h */
|
---|
257 |
|
---|