1 | /* $Id: VBoxServiceControl.cpp 29594 2010-05-18 07:45:58Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceControl - Host-driven Guest Control.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2010 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 <iprt/asm.h>
|
---|
23 | #include <iprt/assert.h>
|
---|
24 | #include <iprt/getopt.h>
|
---|
25 | #include <iprt/mem.h>
|
---|
26 | #include <iprt/semaphore.h>
|
---|
27 | #include <iprt/thread.h>
|
---|
28 | #include <VBox/VBoxGuestLib.h>
|
---|
29 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
30 | #include "VBoxServiceInternal.h"
|
---|
31 | #include "VBoxServiceUtils.h"
|
---|
32 |
|
---|
33 | using namespace guestControl;
|
---|
34 |
|
---|
35 | /*******************************************************************************
|
---|
36 | * Global Variables *
|
---|
37 | *******************************************************************************/
|
---|
38 | /** The control interval (millseconds). */
|
---|
39 | uint32_t g_ControlInterval = 0;
|
---|
40 | /** The semaphore we're blocking on. */
|
---|
41 | static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
42 | /** The guest property service client ID. */
|
---|
43 | static uint32_t g_GuestControlSvcClientID = 0;
|
---|
44 | /** List of spawned processes */
|
---|
45 | RTLISTNODE g_GuestControlExecThreads;
|
---|
46 |
|
---|
47 |
|
---|
48 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
49 | static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
|
---|
50 | {
|
---|
51 | return VINF_SUCCESS;
|
---|
52 | }
|
---|
53 |
|
---|
54 |
|
---|
55 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
56 | static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
57 | {
|
---|
58 | int rc = -1;
|
---|
59 | if (ppszShort)
|
---|
60 | /* no short options */;
|
---|
61 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
62 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
63 | &g_ControlInterval, 1, UINT32_MAX - 1);
|
---|
64 | return rc;
|
---|
65 | }
|
---|
66 |
|
---|
67 |
|
---|
68 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
69 | static DECLCALLBACK(int) VBoxServiceControlInit(void)
|
---|
70 | {
|
---|
71 | /*
|
---|
72 | * If not specified, find the right interval default.
|
---|
73 | * Then create the event sem to block on.
|
---|
74 | */
|
---|
75 | if (!g_ControlInterval)
|
---|
76 | g_ControlInterval = 1000;
|
---|
77 |
|
---|
78 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
79 | AssertRCReturn(rc, rc);
|
---|
80 |
|
---|
81 | rc = VbglR3GuestCtrlConnect(&g_GuestControlSvcClientID);
|
---|
82 | if (RT_SUCCESS(rc))
|
---|
83 | {
|
---|
84 | VBoxServiceVerbose(3, "Control: Service Client ID: %#x\n", g_GuestControlSvcClientID);
|
---|
85 |
|
---|
86 | /* Init thread list. */
|
---|
87 | RTListInit(&g_GuestControlExecThreads);
|
---|
88 | }
|
---|
89 | else
|
---|
90 | {
|
---|
91 | /* If the service was not found, we disable this service without
|
---|
92 | causing VBoxService to fail. */
|
---|
93 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
94 | {
|
---|
95 | VBoxServiceVerbose(0, "Control: Guest control service is not available\n");
|
---|
96 | rc = VERR_SERVICE_DISABLED;
|
---|
97 | }
|
---|
98 | else
|
---|
99 | VBoxServiceError("Control: Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
100 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
101 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
102 | }
|
---|
103 | return rc;
|
---|
104 | }
|
---|
105 |
|
---|
106 |
|
---|
107 | static int VBoxServiceControlHandleCmdStartProcess(uint32_t u32ClientId, uint32_t uNumParms)
|
---|
108 | {
|
---|
109 | uint32_t uContextID;
|
---|
110 | char szCmd[_1K];
|
---|
111 | uint32_t uFlags;
|
---|
112 | char szArgs[_1K];
|
---|
113 | uint32_t uNumArgs;
|
---|
114 | char szEnv[_64K];
|
---|
115 | uint32_t cbEnv = sizeof(szEnv);
|
---|
116 | uint32_t uNumEnvVars;
|
---|
117 | char szUser[128];
|
---|
118 | char szPassword[128];
|
---|
119 | uint32_t uTimeLimitMS;
|
---|
120 |
|
---|
121 | if (uNumParms != 11)
|
---|
122 | return VERR_INVALID_PARAMETER;
|
---|
123 |
|
---|
124 | int rc = VbglR3GuestCtrlExecGetHostCmd(u32ClientId,
|
---|
125 | uNumParms,
|
---|
126 | &uContextID,
|
---|
127 | /* Command */
|
---|
128 | szCmd, sizeof(szCmd),
|
---|
129 | /* Flags */
|
---|
130 | &uFlags,
|
---|
131 | /* Arguments */
|
---|
132 | szArgs, sizeof(szArgs), &uNumArgs,
|
---|
133 | /* Environment */
|
---|
134 | szEnv, &cbEnv, &uNumEnvVars,
|
---|
135 | /* Credentials */
|
---|
136 | szUser, sizeof(szUser),
|
---|
137 | szPassword, sizeof(szPassword),
|
---|
138 | /* Timelimit */
|
---|
139 | &uTimeLimitMS);
|
---|
140 | if (RT_FAILURE(rc))
|
---|
141 | {
|
---|
142 | VBoxServiceError("Control: Failed to retrieve exec start command! Error: %Rrc\n", rc);
|
---|
143 | }
|
---|
144 | else
|
---|
145 | {
|
---|
146 | rc = VBoxServiceControlExecProcess(uContextID, szCmd, uFlags, szArgs, uNumArgs,
|
---|
147 | szEnv, cbEnv, uNumEnvVars,
|
---|
148 | szUser, szPassword, uTimeLimitMS);
|
---|
149 | }
|
---|
150 |
|
---|
151 | VBoxServiceVerbose(3, "Control: VBoxServiceControlHandleCmdStartProcess returned with %Rrc\n", rc);
|
---|
152 | return rc;
|
---|
153 | }
|
---|
154 |
|
---|
155 |
|
---|
156 | static int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms)
|
---|
157 | {
|
---|
158 | uint32_t uContextID;
|
---|
159 | uint32_t uPID;
|
---|
160 | uint32_t uHandleID;
|
---|
161 | uint32_t uFlags;
|
---|
162 |
|
---|
163 | int rc = VbglR3GuestCtrlExecGetHostCmdOutput(u32ClientId, uNumParms,
|
---|
164 | &uContextID, &uPID, &uHandleID, &uFlags);
|
---|
165 | if (RT_FAILURE(rc))
|
---|
166 | {
|
---|
167 | VBoxServiceError("Control: Failed to retrieve exec output command! Error: %Rrc\n", rc);
|
---|
168 | }
|
---|
169 | else
|
---|
170 | {
|
---|
171 | /* Let's have a look if we have a running process with PID = uPID ... */
|
---|
172 | PVBOXSERVICECTRLTHREAD pNode;
|
---|
173 | bool fFound = false;
|
---|
174 | RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
|
---|
175 | {
|
---|
176 | if ( pNode->fStarted
|
---|
177 | && pNode->enmType == VBoxServiceCtrlThreadDataExec)
|
---|
178 | {
|
---|
179 | PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
|
---|
180 | if (pData && pData->uPID == uPID)
|
---|
181 | {
|
---|
182 | fFound = true;
|
---|
183 | break;
|
---|
184 | }
|
---|
185 | }
|
---|
186 | }
|
---|
187 |
|
---|
188 | if (fFound)
|
---|
189 | {
|
---|
190 | PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
|
---|
191 | AssertPtr(pData);
|
---|
192 |
|
---|
193 | const uint32_t cbSize = _4K;
|
---|
194 | uint32_t cbRead = cbSize;
|
---|
195 | uint8_t *pBuf = (uint8_t*)RTMemAlloc(cbSize);
|
---|
196 | if (pBuf)
|
---|
197 | {
|
---|
198 | rc = VBoxServiceControlExecReadPipeBufferContent(&pData->stdOut, pBuf, cbSize, &cbRead);
|
---|
199 | if (RT_SUCCESS(rc))
|
---|
200 | {
|
---|
201 | /* cbRead now contains actual size. */
|
---|
202 | rc = VbglR3GuestCtrlExecSendOut(u32ClientId, uContextID, uPID, 0 /* handle ID */, 0 /* flags */,
|
---|
203 | pBuf, cbRead);
|
---|
204 | }
|
---|
205 | RTMemFree(pBuf);
|
---|
206 | }
|
---|
207 | else
|
---|
208 | rc = VERR_NO_MEMORY;
|
---|
209 | }
|
---|
210 | else
|
---|
211 | rc = VERR_NOT_FOUND; /* PID not found! */
|
---|
212 | }
|
---|
213 | VBoxServiceVerbose(3, "Control: VBoxServiceControlHandleCmdGetOutput returned with %Rrc\n", rc);
|
---|
214 | return rc;
|
---|
215 | }
|
---|
216 |
|
---|
217 |
|
---|
218 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
219 | DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
|
---|
220 | {
|
---|
221 | /*
|
---|
222 | * Tell the control thread that it can continue
|
---|
223 | * spawning services.
|
---|
224 | */
|
---|
225 | RTThreadUserSignal(RTThreadSelf());
|
---|
226 | Assert(g_GuestControlSvcClientID > 0);
|
---|
227 |
|
---|
228 | int rc = VINF_SUCCESS;
|
---|
229 |
|
---|
230 | /*
|
---|
231 | * Execution loop.
|
---|
232 | *
|
---|
233 | * @todo
|
---|
234 | */
|
---|
235 | for (;;)
|
---|
236 | {
|
---|
237 | uint32_t uMsg;
|
---|
238 | uint32_t uNumParms;
|
---|
239 | VBoxServiceVerbose(3, "Control: Waiting for host msg ...\n");
|
---|
240 | rc = VbglR3GuestCtrlGetHostMsg(g_GuestControlSvcClientID, &uMsg, &uNumParms);
|
---|
241 | if (RT_FAILURE(rc))
|
---|
242 | {
|
---|
243 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
244 | {
|
---|
245 | VBoxServiceVerbose(4, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", uNumParms);
|
---|
246 | rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
|
---|
247 | }
|
---|
248 | else
|
---|
249 | VBoxServiceVerbose(3, "Control: Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
|
---|
250 | }
|
---|
251 |
|
---|
252 | if (RT_SUCCESS(rc))
|
---|
253 | {
|
---|
254 | VBoxServiceVerbose(3, "Control: Msg=%u (%u parms) retrieved\n", uMsg, uNumParms);
|
---|
255 | switch(uMsg)
|
---|
256 | {
|
---|
257 | case GETHOSTMSG_EXEC_HOST_CANCEL_WAIT:
|
---|
258 | VBoxServiceVerbose(3, "Control: Host asked us to quit ...\n");
|
---|
259 | break;
|
---|
260 |
|
---|
261 | case GETHOSTMSG_EXEC_START_PROCESS:
|
---|
262 | rc = VBoxServiceControlHandleCmdStartProcess(g_GuestControlSvcClientID, uNumParms);
|
---|
263 | break;
|
---|
264 |
|
---|
265 | case GETHOSTMSG_EXEC_GET_OUTPUT:
|
---|
266 | rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, uNumParms);
|
---|
267 | break;
|
---|
268 |
|
---|
269 | default:
|
---|
270 | VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
|
---|
271 | /* Don't terminate here; just wait for the next message. */
|
---|
272 | break;
|
---|
273 | }
|
---|
274 |
|
---|
275 | if (RT_FAILURE(rc))
|
---|
276 | VBoxServiceVerbose(3, "Control: Message was processed with rc=%Rrc\n", rc);
|
---|
277 | }
|
---|
278 |
|
---|
279 | /* Do we need to shutdown? */
|
---|
280 | if ( *pfShutdown
|
---|
281 | || uMsg == GETHOSTMSG_EXEC_HOST_CANCEL_WAIT)
|
---|
282 | {
|
---|
283 | rc = VINF_SUCCESS;
|
---|
284 | break;
|
---|
285 | }
|
---|
286 |
|
---|
287 | /* Let's sleep for a bit and let others run ... */
|
---|
288 | RTThreadYield();
|
---|
289 | }
|
---|
290 |
|
---|
291 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
292 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
298 | static DECLCALLBACK(void) VBoxServiceControlStop(void)
|
---|
299 | {
|
---|
300 | VBoxServiceVerbose(3, "Control: Stopping ...\n");
|
---|
301 |
|
---|
302 | /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
|
---|
303 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
304 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
305 |
|
---|
306 | /*
|
---|
307 | * Ask the host service to cancel all pending requests so that we can
|
---|
308 | * shutdown properly here.
|
---|
309 | */
|
---|
310 | if (g_GuestControlSvcClientID)
|
---|
311 | {
|
---|
312 | int rc = VbglR3GuestCtrlCancelPendingWaits(g_GuestControlSvcClientID);
|
---|
313 | if (RT_FAILURE(rc))
|
---|
314 | VBoxServiceError("Control: Cancelling pending waits failed; rc=%Rrc\n", rc);
|
---|
315 | }
|
---|
316 | }
|
---|
317 |
|
---|
318 |
|
---|
319 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
320 | static DECLCALLBACK(void) VBoxServiceControlTerm(void)
|
---|
321 | {
|
---|
322 | VBoxServiceVerbose(3, "Control: Terminating ...\n");
|
---|
323 |
|
---|
324 | /* Signal all threads that we want to shutdown. */
|
---|
325 | PVBOXSERVICECTRLTHREAD pNode;
|
---|
326 | RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
|
---|
327 | ASMAtomicXchgBool(&pNode->fShutdown, true);
|
---|
328 |
|
---|
329 | /* Wait for threads to shutdown. */
|
---|
330 | RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
|
---|
331 | {
|
---|
332 | if (pNode->Thread != NIL_RTTHREAD)
|
---|
333 | {
|
---|
334 | /* Wait a bit ... */
|
---|
335 | int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
|
---|
336 | if (RT_FAILURE(rc2))
|
---|
337 | VBoxServiceError("Control: Thread failed to stop; rc2=%Rrc\n", rc2);
|
---|
338 | }
|
---|
339 |
|
---|
340 | /* Destroy thread specific data. */
|
---|
341 | switch (pNode->enmType)
|
---|
342 | {
|
---|
343 | case VBoxServiceCtrlThreadDataExec:
|
---|
344 | VBoxServiceControlExecDestroyThreadData((PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData);
|
---|
345 | break;
|
---|
346 |
|
---|
347 | default:
|
---|
348 | break;
|
---|
349 | }
|
---|
350 | }
|
---|
351 |
|
---|
352 | /* Finally destroy thread list. */
|
---|
353 | pNode = RTListNodeGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
|
---|
354 | while (pNode)
|
---|
355 | {
|
---|
356 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
357 |
|
---|
358 | RTListNodeRemove(&pNode->Node);
|
---|
359 | RTMemFree(pNode);
|
---|
360 |
|
---|
361 | if (pNext && RTListNodeIsLast(&g_GuestControlExecThreads, &pNext->Node))
|
---|
362 | break;
|
---|
363 |
|
---|
364 | pNode = pNext;
|
---|
365 | }
|
---|
366 |
|
---|
367 | VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
|
---|
368 | g_GuestControlSvcClientID = 0;
|
---|
369 |
|
---|
370 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
371 | {
|
---|
372 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
373 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
374 | }
|
---|
375 | }
|
---|
376 |
|
---|
377 |
|
---|
378 | /**
|
---|
379 | * The 'vminfo' service description.
|
---|
380 | */
|
---|
381 | VBOXSERVICE g_Control =
|
---|
382 | {
|
---|
383 | /* pszName. */
|
---|
384 | "control",
|
---|
385 | /* pszDescription. */
|
---|
386 | "Host-driven Guest Control",
|
---|
387 | /* pszUsage. */
|
---|
388 | " [--control-interval <ms>]"
|
---|
389 | ,
|
---|
390 | /* pszOptions. */
|
---|
391 | " --control-interval Specifies the interval at which to check for\n"
|
---|
392 | " new control commands. The default is 1000 ms.\n"
|
---|
393 | ,
|
---|
394 | /* methods */
|
---|
395 | VBoxServiceControlPreInit,
|
---|
396 | VBoxServiceControlOption,
|
---|
397 | VBoxServiceControlInit,
|
---|
398 | VBoxServiceControlWorker,
|
---|
399 | VBoxServiceControlStop,
|
---|
400 | VBoxServiceControlTerm
|
---|
401 | };
|
---|
402 |
|
---|