1 |
|
---|
2 | /* $Id: VBoxModAPIMonitor.cpp 39988 2012-02-03 16:23:24Z vboxsync $ */
|
---|
3 | /** @file
|
---|
4 | * VBoxModAPIMonitor - API monitor module for detecting host isolation.
|
---|
5 | */
|
---|
6 |
|
---|
7 | /*
|
---|
8 | * Copyright (C) 2012 Oracle Corporation
|
---|
9 | *
|
---|
10 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
11 | * available from http://www.virtualbox.org. This file is free software;
|
---|
12 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
13 | * General Public License (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
17 | */
|
---|
18 |
|
---|
19 |
|
---|
20 | /*******************************************************************************
|
---|
21 | * Header Files *
|
---|
22 | *******************************************************************************/
|
---|
23 | #ifndef VBOX_ONLY_DOCS
|
---|
24 | # include <VBox/com/com.h>
|
---|
25 | # include <VBox/com/string.h>
|
---|
26 | # include <VBox/com/Guid.h>
|
---|
27 | # include <VBox/com/array.h>
|
---|
28 | # include <VBox/com/ErrorInfo.h>
|
---|
29 | # include <VBox/com/errorprint.h>
|
---|
30 |
|
---|
31 | # include <VBox/com/EventQueue.h>
|
---|
32 | # include <VBox/com/listeners.h>
|
---|
33 | # include <VBox/com/VirtualBox.h>
|
---|
34 | #endif /* !VBOX_ONLY_DOCS */
|
---|
35 |
|
---|
36 | #include <VBox/err.h>
|
---|
37 | #include <VBox/log.h>
|
---|
38 | #include <VBox/version.h>
|
---|
39 |
|
---|
40 | #include <package-generated.h>
|
---|
41 |
|
---|
42 | #include <iprt/asm.h>
|
---|
43 | #include <iprt/buildconfig.h>
|
---|
44 | #include <iprt/critsect.h>
|
---|
45 | #include <iprt/getopt.h>
|
---|
46 | #include <iprt/initterm.h>
|
---|
47 | #include <iprt/path.h>
|
---|
48 | #include <iprt/process.h>
|
---|
49 | #include <iprt/semaphore.h>
|
---|
50 | #include <iprt/stream.h>
|
---|
51 | #include <iprt/string.h>
|
---|
52 | #include <iprt/system.h>
|
---|
53 |
|
---|
54 | #include <map>
|
---|
55 | #include <string>
|
---|
56 | #include <signal.h>
|
---|
57 |
|
---|
58 | #include "VBoxWatchdogInternal.h"
|
---|
59 |
|
---|
60 | using namespace com;
|
---|
61 |
|
---|
62 | #define VBOX_MOD_APIMON_NAME "apimon"
|
---|
63 |
|
---|
64 | /**
|
---|
65 | * The module's RTGetOpt-IDs for the command line.
|
---|
66 | */
|
---|
67 | enum GETOPTDEF_APIMON
|
---|
68 | {
|
---|
69 | GETOPTDEF_APIMON_ISLN_RESPONSE = 1000,
|
---|
70 | GETOPTDEF_APIMON_ISLN_TIMEOUT,
|
---|
71 | GETOPTDEF_APIMON_GROUPS
|
---|
72 | };
|
---|
73 |
|
---|
74 | /**
|
---|
75 | * The module's command line arguments.
|
---|
76 | */
|
---|
77 | static const RTGETOPTDEF g_aAPIMonitorOpts[] = {
|
---|
78 | { "--apimon-isln-response", GETOPTDEF_APIMON_ISLN_RESPONSE, RTGETOPT_REQ_STRING },
|
---|
79 | { "--apimon-isln-timeout", GETOPTDEF_APIMON_ISLN_TIMEOUT, RTGETOPT_REQ_UINT32 },
|
---|
80 | { "--apimon-groups", GETOPTDEF_APIMON_GROUPS, RTGETOPT_REQ_STRING }
|
---|
81 | };
|
---|
82 |
|
---|
83 | static unsigned long g_ulAPIMonIslnTimeoutMS = 0;
|
---|
84 | static Bstr g_strAPIMonGroups;
|
---|
85 | static Bstr g_strAPIMonIslnCmdResp;
|
---|
86 |
|
---|
87 | /* Callbacks. */
|
---|
88 | static DECLCALLBACK(int) VBoxModAPIMonitorPreInit(void)
|
---|
89 | {
|
---|
90 | return VINF_SUCCESS;
|
---|
91 | }
|
---|
92 |
|
---|
93 | static DECLCALLBACK(int) VBoxModAPIMonitorOption(int argc, char **argv)
|
---|
94 | {
|
---|
95 | if (!argc) /* Take a shortcut. */
|
---|
96 | return -1;
|
---|
97 |
|
---|
98 | AssertPtrReturn(argv, VERR_INVALID_PARAMETER);
|
---|
99 |
|
---|
100 | RTGETOPTSTATE GetState;
|
---|
101 | int rc = RTGetOptInit(&GetState, argc, argv,
|
---|
102 | g_aAPIMonitorOpts, RT_ELEMENTS(g_aAPIMonitorOpts),
|
---|
103 | 0 /* First */, 0 /*fFlags*/);
|
---|
104 | if (RT_FAILURE(rc))
|
---|
105 | return rc;
|
---|
106 |
|
---|
107 | rc = 0; /* Set default parsing result to valid. */
|
---|
108 |
|
---|
109 | int c;
|
---|
110 | RTGETOPTUNION ValueUnion;
|
---|
111 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
112 | {
|
---|
113 | switch (c)
|
---|
114 | {
|
---|
115 | case GETOPTDEF_APIMON_ISLN_RESPONSE:
|
---|
116 | break;
|
---|
117 |
|
---|
118 | case GETOPTDEF_APIMON_ISLN_TIMEOUT:
|
---|
119 | g_ulAPIMonIslnTimeoutMS = ValueUnion.u32;
|
---|
120 | if (g_ulAPIMonIslnTimeoutMS < 1000)
|
---|
121 | g_ulAPIMonIslnTimeoutMS = 1000;
|
---|
122 | break;
|
---|
123 |
|
---|
124 | case GETOPTDEF_APIMON_GROUPS:
|
---|
125 | break;
|
---|
126 |
|
---|
127 | default:
|
---|
128 | rc = -1; /* We don't handle this option, skip. */
|
---|
129 | break;
|
---|
130 | }
|
---|
131 | }
|
---|
132 |
|
---|
133 | return rc;
|
---|
134 | }
|
---|
135 |
|
---|
136 | static DECLCALLBACK(int) VBoxModAPIMonitorInit(void)
|
---|
137 | {
|
---|
138 | HRESULT rc = S_OK;
|
---|
139 |
|
---|
140 | do
|
---|
141 | {
|
---|
142 | Bstr bstrValue;
|
---|
143 |
|
---|
144 | /* Host isolation timeout (in ms). */
|
---|
145 | if (!g_ulAPIMonIslnTimeoutMS) /* Not set by command line? */
|
---|
146 | {
|
---|
147 | CHECK_ERROR_BREAK(g_pVirtualBox, GetExtraData(Bstr("VBoxInternal2/Watchdog/APIMonitor/IsolationTimeout").raw(),
|
---|
148 | bstrValue.asOutParam()));
|
---|
149 | g_ulAPIMonIslnTimeoutMS = Utf8Str(bstrValue).toUInt32();
|
---|
150 | }
|
---|
151 | if (!g_ulAPIMonIslnTimeoutMS)
|
---|
152 | {
|
---|
153 | /* Default is 30 seconds timeout. */
|
---|
154 | g_ulAPIMonIslnTimeoutMS = 30 * 1000;
|
---|
155 | }
|
---|
156 |
|
---|
157 | /* VM groups to watch for. */
|
---|
158 | if (g_strAPIMonGroups.isEmpty()) /* Not set by command line? */
|
---|
159 | {
|
---|
160 | CHECK_ERROR_BREAK(g_pVirtualBox, GetExtraData(Bstr("VBoxInternal2/Watchdog/APIMonitor/Groups").raw(),
|
---|
161 | g_strAPIMonGroups.asOutParam()));
|
---|
162 | }
|
---|
163 |
|
---|
164 | /* Host isolation command response. */
|
---|
165 | if (g_strAPIMonIslnCmdResp.isEmpty()) /* Not set by command line? */
|
---|
166 | {
|
---|
167 | CHECK_ERROR_BREAK(g_pVirtualBox, GetExtraData(Bstr("VBoxInternal2/Watchdog/APIMonitor/IsolationResponse").raw(),
|
---|
168 | g_strAPIMonIslnCmdResp.asOutParam()));
|
---|
169 | }
|
---|
170 | } while (0);
|
---|
171 |
|
---|
172 | return SUCCEEDED(rc) ? VINF_SUCCESS : VERR_COM_IPRT_ERROR; /* @todo Find a better rc! */
|
---|
173 | }
|
---|
174 |
|
---|
175 | static DECLCALLBACK(int) VBoxModAPIMonitorMain(void)
|
---|
176 | {
|
---|
177 | return VINF_SUCCESS; /* Nothing to do here right now. */
|
---|
178 | }
|
---|
179 |
|
---|
180 | static DECLCALLBACK(int) VBoxModAPIMonitorStop(void)
|
---|
181 | {
|
---|
182 | return VINF_SUCCESS;
|
---|
183 | }
|
---|
184 |
|
---|
185 | static DECLCALLBACK(void) VBoxModAPIMonitorTerm(void)
|
---|
186 | {
|
---|
187 | }
|
---|
188 |
|
---|
189 | static DECLCALLBACK(int) VBoxModAPIMonitorOnMachineRegistered(const Bstr &strUuid)
|
---|
190 | {
|
---|
191 | return VINF_SUCCESS;
|
---|
192 | }
|
---|
193 |
|
---|
194 | static DECLCALLBACK(int) VBoxModAPIMonitorOnMachineUnregistered(const Bstr &strUuid)
|
---|
195 | {
|
---|
196 | return VINF_SUCCESS;
|
---|
197 | }
|
---|
198 |
|
---|
199 | static DECLCALLBACK(int) VBoxModAPIMonitorOnMachineStateChanged(const Bstr &strUuid,
|
---|
200 | MachineState_T enmState)
|
---|
201 | {
|
---|
202 | return VINF_SUCCESS;
|
---|
203 | }
|
---|
204 |
|
---|
205 | static DECLCALLBACK(int) VBoxModAPIMonitorOnServiceStateChanged(bool fAvailable)
|
---|
206 | {
|
---|
207 | return VINF_SUCCESS;
|
---|
208 | }
|
---|
209 |
|
---|
210 | /**
|
---|
211 | * The 'apimonitor' module description.
|
---|
212 | */
|
---|
213 | VBOXMODULE g_ModAPIMonitor =
|
---|
214 | {
|
---|
215 | /* pszName. */
|
---|
216 | VBOX_MOD_APIMON_NAME,
|
---|
217 | /* pszDescription. */
|
---|
218 | "API monitor for host isolation detection",
|
---|
219 | /* pszDepends. */
|
---|
220 | NULL,
|
---|
221 | /* uPriority. */
|
---|
222 | 0 /* Not used */,
|
---|
223 | /* pszUsage. */
|
---|
224 | " [--apimon-isln-response=<cmd>] [--apimon-isln-timeout=<ms>]\n"
|
---|
225 | " [--apimon-groups=<string>]\n",
|
---|
226 | /* pszOptions. */
|
---|
227 | "--apimon-isln-response Sets the isolation response (shutdown VM).\n"
|
---|
228 | "--apimon-isln-timeout Sets the isolation timeout in ms (none).\n"
|
---|
229 | "--apimon-groups Sets the VM groups for monitoring (none).\n",
|
---|
230 | /* methods. */
|
---|
231 | VBoxModAPIMonitorPreInit,
|
---|
232 | VBoxModAPIMonitorOption,
|
---|
233 | VBoxModAPIMonitorInit,
|
---|
234 | VBoxModAPIMonitorMain,
|
---|
235 | VBoxModAPIMonitorStop,
|
---|
236 | VBoxModAPIMonitorTerm,
|
---|
237 | /* callbacks. */
|
---|
238 | VBoxModAPIMonitorOnMachineRegistered,
|
---|
239 | VBoxModAPIMonitorOnMachineUnregistered,
|
---|
240 | VBoxModAPIMonitorOnMachineStateChanged,
|
---|
241 | VBoxModAPIMonitorOnServiceStateChanged
|
---|
242 | };
|
---|
243 |
|
---|