VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.cpp@ 28833

Last change on this file since 28833 was 28833, checked in by vboxsync, 15 years ago

Guest Control: Bugfixes.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette