1 | /* $Id: */
|
---|
2 | /** @file
|
---|
3 | * VBoxWatchdogUtils - Misc. utility functions for modules.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2011-2020 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <VBox/com/array.h>
|
---|
23 | #include "VBoxWatchdogInternal.h"
|
---|
24 |
|
---|
25 | #include <iprt/sanitized/sstream>
|
---|
26 | #include <algorithm>
|
---|
27 |
|
---|
28 |
|
---|
29 | /**
|
---|
30 | * Adds a group / a set of groups to the specified map.
|
---|
31 | * If a group in the group map exists there will be no action.
|
---|
32 | *
|
---|
33 | * @return IPRT status code.
|
---|
34 | * @param groups Map to add group(s) to.
|
---|
35 | * @param pszGroupsToAdd Comma-separated string of one or more groups to add.
|
---|
36 | * @param fFlags Flags to set to the groups added.
|
---|
37 | */
|
---|
38 | int groupAdd(mapGroups &groups, const char *pszGroupsToAdd, uint32_t fFlags)
|
---|
39 | {
|
---|
40 | AssertPtrReturn(pszGroupsToAdd, VERR_INVALID_POINTER);
|
---|
41 |
|
---|
42 | try
|
---|
43 | {
|
---|
44 | std::istringstream strGroups(pszGroupsToAdd);
|
---|
45 | for(std::string strToken; getline(strGroups, strToken, ','); )
|
---|
46 | {
|
---|
47 | strToken.erase(remove_if(strToken.begin(), strToken.end(), isspace), strToken.end());
|
---|
48 |
|
---|
49 | Utf8Str strTokenUtf8(strToken.c_str());
|
---|
50 | mapGroupsIterConst it = groups.find(strTokenUtf8);
|
---|
51 |
|
---|
52 | if (it == groups.end())
|
---|
53 | groups.insert(std::make_pair(strTokenUtf8, fFlags));
|
---|
54 | }
|
---|
55 | }
|
---|
56 | catch (...)
|
---|
57 | {
|
---|
58 | AssertFailed();
|
---|
59 | }
|
---|
60 |
|
---|
61 | return VINF_SUCCESS;
|
---|
62 | }
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * Retrieves a metric from a specified machine.
|
---|
66 | *
|
---|
67 | * @return IPRT status code.
|
---|
68 | * @param pMachine Pointer to the machine's internal structure.
|
---|
69 | * @param strName Name of metric to retrieve.
|
---|
70 | * @param pulData Pointer to value to retrieve the actual metric value.
|
---|
71 | */
|
---|
72 | int getMetric(PVBOXWATCHDOG_MACHINE pMachine, const Bstr& strName, LONG *pulData)
|
---|
73 | {
|
---|
74 | AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
|
---|
75 | AssertPtrReturn(pulData, VERR_INVALID_POINTER);
|
---|
76 |
|
---|
77 | /* Input. */
|
---|
78 | com::SafeArray<BSTR> metricNames(1);
|
---|
79 | com::SafeIfaceArray<IUnknown> metricObjects(1);
|
---|
80 | pMachine->machine.queryInterfaceTo(&metricObjects[0]);
|
---|
81 |
|
---|
82 | /* Output. */
|
---|
83 | com::SafeArray<BSTR> retNames;
|
---|
84 | com::SafeIfaceArray<IUnknown> retObjects;
|
---|
85 | com::SafeArray<BSTR> retUnits;
|
---|
86 | com::SafeArray<ULONG> retScales;
|
---|
87 | com::SafeArray<ULONG> retSequenceNumbers;
|
---|
88 | com::SafeArray<ULONG> retIndices;
|
---|
89 | com::SafeArray<ULONG> retLengths;
|
---|
90 | com::SafeArray<LONG> retData;
|
---|
91 |
|
---|
92 | /* Query current memory free. */
|
---|
93 | strName.cloneTo(&metricNames[0]);
|
---|
94 | #ifdef VBOX_WATCHDOG_GLOBAL_PERFCOL
|
---|
95 | Assert(!g_pPerfCollector.isNull());
|
---|
96 | HRESULT hrc = g_pPerfCollector->QueryMetricsData(
|
---|
97 | #else
|
---|
98 | Assert(!pMachine->collector.isNull());
|
---|
99 | HRESULT hrc = pMachine->collector->QueryMetricsData(
|
---|
100 | #endif
|
---|
101 | ComSafeArrayAsInParam(metricNames),
|
---|
102 | ComSafeArrayAsInParam(metricObjects),
|
---|
103 | ComSafeArrayAsOutParam(retNames),
|
---|
104 | ComSafeArrayAsOutParam(retObjects),
|
---|
105 | ComSafeArrayAsOutParam(retUnits),
|
---|
106 | ComSafeArrayAsOutParam(retScales),
|
---|
107 | ComSafeArrayAsOutParam(retSequenceNumbers),
|
---|
108 | ComSafeArrayAsOutParam(retIndices),
|
---|
109 | ComSafeArrayAsOutParam(retLengths),
|
---|
110 | ComSafeArrayAsOutParam(retData));
|
---|
111 | #if 0
|
---|
112 | /* Useful for metrics debugging. */
|
---|
113 | for (unsigned j = 0; j < retNames.size(); j++)
|
---|
114 | {
|
---|
115 | Bstr metricUnit(retUnits[j]);
|
---|
116 | Bstr metricName(retNames[j]);
|
---|
117 | RTPrintf("%-20ls ", metricName.raw());
|
---|
118 | const char *separator = "";
|
---|
119 | for (unsigned k = 0; k < retLengths[j]; k++)
|
---|
120 | {
|
---|
121 | if (retScales[j] == 1)
|
---|
122 | RTPrintf("%s%d %ls", separator, retData[retIndices[j] + k], metricUnit.raw());
|
---|
123 | else
|
---|
124 | RTPrintf("%s%d.%02d%ls", separator, retData[retIndices[j] + k] / retScales[j],
|
---|
125 | (retData[retIndices[j] + k] * 100 / retScales[j]) % 100, metricUnit.raw());
|
---|
126 | separator = ", ";
|
---|
127 | }
|
---|
128 | RTPrintf("\n");
|
---|
129 | }
|
---|
130 | #endif
|
---|
131 |
|
---|
132 | if (SUCCEEDED(hrc))
|
---|
133 | *pulData = retData.size() ? retData[retIndices[0]] : 0;
|
---|
134 |
|
---|
135 | return SUCCEEDED(hrc) ? VINF_SUCCESS : VINF_NOT_SUPPORTED;
|
---|
136 | }
|
---|
137 |
|
---|
138 | /**
|
---|
139 | * Returns the payload of a machine.
|
---|
140 | *
|
---|
141 | * @return void* Pointer to payload data. Mutable!
|
---|
142 | * @param pMachine Machine to get payload for.
|
---|
143 | * @param pszModule Module name to get payload from.
|
---|
144 | */
|
---|
145 | void* payloadFrom(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
|
---|
146 | {
|
---|
147 | AssertPtrReturn(pMachine, NULL);
|
---|
148 | AssertPtrReturn(pszModule, NULL);
|
---|
149 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
150 | if (it == pMachine->payload.end())
|
---|
151 | return NULL;
|
---|
152 | Assert(it->second.cbData);
|
---|
153 | return it->second.pvData;
|
---|
154 | }
|
---|
155 |
|
---|
156 | int payloadAlloc(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule,
|
---|
157 | size_t cbSize, void **ppszPayload)
|
---|
158 | {
|
---|
159 | AssertPtrReturn(pMachine, VERR_INVALID_POINTER);
|
---|
160 | AssertPtrReturn(pszModule, VERR_INVALID_POINTER);
|
---|
161 | AssertReturn(cbSize, VERR_INVALID_PARAMETER);
|
---|
162 |
|
---|
163 | void *pvData = RTMemAllocZ(cbSize);
|
---|
164 | AssertPtrReturn(pvData, VERR_NO_MEMORY);
|
---|
165 |
|
---|
166 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
167 | AssertReturn(it == pMachine->payload.end(), VERR_INVALID_PARAMETER);
|
---|
168 |
|
---|
169 | VBOXWATCHDOG_MODULE_PAYLOAD p;
|
---|
170 | p.pvData = pvData;
|
---|
171 | p.cbData = cbSize;
|
---|
172 |
|
---|
173 | if (ppszPayload)
|
---|
174 | *ppszPayload = p.pvData;
|
---|
175 |
|
---|
176 | pMachine->payload.insert(std::make_pair(pszModule, p));
|
---|
177 |
|
---|
178 | return VINF_SUCCESS;
|
---|
179 | }
|
---|
180 |
|
---|
181 | void payloadFree(PVBOXWATCHDOG_MACHINE pMachine, const char *pszModule)
|
---|
182 | {
|
---|
183 | AssertPtrReturnVoid(pMachine);
|
---|
184 | AssertPtrReturnVoid(pszModule);
|
---|
185 |
|
---|
186 | mapPayloadIter it = pMachine->payload.find(pszModule);
|
---|
187 | if (it != pMachine->payload.end())
|
---|
188 | {
|
---|
189 | RTMemFree(it->second.pvData);
|
---|
190 | pMachine->payload.erase(it);
|
---|
191 | }
|
---|
192 | }
|
---|
193 |
|
---|
194 | PVBOXWATCHDOG_MACHINE getMachine(const Bstr& strUuid)
|
---|
195 | {
|
---|
196 | mapVMIter it = g_mapVM.find(strUuid);
|
---|
197 | if (it != g_mapVM.end())
|
---|
198 | return &it->second;
|
---|
199 | return NULL;
|
---|
200 | }
|
---|
201 |
|
---|
202 | MachineState_T getMachineState(const PVBOXWATCHDOG_MACHINE pMachine)
|
---|
203 | {
|
---|
204 | AssertPtrReturn(pMachine, MachineState_Null);
|
---|
205 | MachineState_T machineState;
|
---|
206 | Assert(!pMachine->machine.isNull());
|
---|
207 | HRESULT rc = pMachine->machine->COMGETTER(State)(&machineState);
|
---|
208 | if (SUCCEEDED(rc))
|
---|
209 | return machineState;
|
---|
210 | return MachineState_Null;
|
---|
211 | }
|
---|
212 |
|
---|
213 | int cfgGetValueStr(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
|
---|
214 | const char *pszGlobal, const char *pszVM, Utf8Str &strValue, Utf8Str strDefault)
|
---|
215 | {
|
---|
216 | AssertReturn(!rptrVBox.isNull(), VERR_INVALID_POINTER);
|
---|
217 |
|
---|
218 |
|
---|
219 | /* Try per-VM approach. */
|
---|
220 | Bstr strTemp;
|
---|
221 | HRESULT hr;
|
---|
222 | if (!rptrMachine.isNull())
|
---|
223 | {
|
---|
224 | AssertPtr(pszVM);
|
---|
225 | hr = rptrMachine->GetExtraData(Bstr(pszVM).raw(),
|
---|
226 | strTemp.asOutParam());
|
---|
227 | if ( SUCCEEDED(hr)
|
---|
228 | && !strTemp.isEmpty())
|
---|
229 | {
|
---|
230 | strValue = Utf8Str(strTemp);
|
---|
231 | }
|
---|
232 | }
|
---|
233 |
|
---|
234 | if (strValue.isEmpty()) /* Not set by per-VM value? */
|
---|
235 | {
|
---|
236 | AssertPtr(pszGlobal);
|
---|
237 |
|
---|
238 | /* Try global approach. */
|
---|
239 | hr = rptrVBox->GetExtraData(Bstr(pszGlobal).raw(),
|
---|
240 | strTemp.asOutParam());
|
---|
241 | if ( SUCCEEDED(hr)
|
---|
242 | && !strTemp.isEmpty())
|
---|
243 | {
|
---|
244 | strValue = Utf8Str(strTemp);
|
---|
245 | }
|
---|
246 | }
|
---|
247 |
|
---|
248 | if (strValue.isEmpty())
|
---|
249 | {
|
---|
250 | strValue = strDefault;
|
---|
251 | return VERR_NOT_FOUND;
|
---|
252 | }
|
---|
253 |
|
---|
254 | return VINF_SUCCESS;
|
---|
255 | }
|
---|
256 |
|
---|
257 | int cfgGetValueU32(const ComPtr<IVirtualBox> &rptrVBox, const ComPtr<IMachine> &rptrMachine,
|
---|
258 | const char *pszGlobal, const char *pszVM, uint32_t *puValue, uint32_t uDefault)
|
---|
259 | {
|
---|
260 | Utf8Str strValue;
|
---|
261 | int rc = cfgGetValueStr(rptrVBox, rptrMachine, pszGlobal, pszVM, strValue, "" /* Default */);
|
---|
262 | if (RT_SUCCESS(rc))
|
---|
263 | *puValue = strValue.toUInt32();
|
---|
264 | else
|
---|
265 | *puValue = uDefault;
|
---|
266 | return rc;
|
---|
267 | }
|
---|
268 |
|
---|