VirtualBox

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

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

VBoxService: Introduced guest property cache (used by VM information thread). Not tested yet.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.5 KB
Line 
1
2/* $Id: VBoxServiceControl.cpp 28967 2010-05-03 11:44:28Z 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 (RT_FAILURE(rc))
242 {
243 if (rc == VERR_TOO_MUCH_DATA)
244 {
245 VBoxServiceVerbose(3, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request ...\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_START_PROCESS:
258 rc = VBoxServiceControlHandleCmdStartProcess(g_GuestControlSvcClientID, uNumParms);
259 break;
260
261 case GETHOSTMSG_EXEC_GET_OUTPUT:
262 rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, uNumParms);
263 break;
264
265 default:
266 VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
267 /* Don't terminate here; just wait for the next message. */
268 break;
269 }
270
271 if (RT_FAILURE(rc))
272 VBoxServiceVerbose(3, "Control: Message was processed with rc=%Rrc\n", rc);
273 }
274
275 /* Do we need to shutdown? */
276 if (*pfShutdown)
277 {
278 rc = 0;
279 break;
280 }
281
282 /* Let's sleep for a bit and let others run ... */
283 RTThreadYield();
284 }
285
286 RTSemEventMultiDestroy(g_hControlEvent);
287 g_hControlEvent = NIL_RTSEMEVENTMULTI;
288 return rc;
289}
290
291
292/** @copydoc VBOXSERVICE::pfnStop */
293static DECLCALLBACK(void) VBoxServiceControlStop(void)
294{
295 /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
296 * annoying call since doesn't support timeouts in the posix world. */
297 RTSemEventMultiSignal(g_hControlEvent);
298}
299
300
301/** @copydoc VBOXSERVICE::pfnTerm */
302static DECLCALLBACK(void) VBoxServiceControlTerm(void)
303{
304 /* Signal all threads that we want to shutdown. */
305 PVBOXSERVICECTRLTHREAD pNode;
306 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
307 ASMAtomicXchgBool(&pNode->fShutdown, true);
308
309 /* Wait for threads to shutdown. */
310 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
311 {
312 if (pNode->Thread != NIL_RTTHREAD)
313 {
314 int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
315 if (RT_FAILURE(rc2))
316 VBoxServiceError("Control: Thread failed to stop; rc2=%Rrc\n", rc2);
317 }
318
319 /* Destroy thread specific data. */
320 switch (pNode->enmType)
321 {
322 case VBoxServiceCtrlThreadDataExec:
323 VBoxServiceControlExecDestroyThreadData((PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData);
324 break;
325
326 default:
327 break;
328 }
329 }
330
331 /* Finally destroy thread list. */
332 pNode = RTListNodeGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
333 while (pNode)
334 {
335 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
336
337 RTListNodeRemove(&pNode->Node);
338 RTMemFree(pNode);
339
340 if (pNext && RTListNodeIsLast(&g_GuestControlExecThreads, &pNext->Node))
341 break;
342
343 pNode = pNext;
344 }
345
346 VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
347 g_GuestControlSvcClientID = 0;
348
349 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
350 {
351 RTSemEventMultiDestroy(g_hControlEvent);
352 g_hControlEvent = NIL_RTSEMEVENTMULTI;
353 }
354}
355
356
357/**
358 * The 'vminfo' service description.
359 */
360VBOXSERVICE g_Control =
361{
362 /* pszName. */
363 "control",
364 /* pszDescription. */
365 "Host-driven Guest Control",
366 /* pszUsage. */
367 "[--control-interval <ms>]"
368 ,
369 /* pszOptions. */
370 " --control-interval Specifies the interval at which to check for\n"
371 " new control commands. The default is 1000 ms.\n"
372 ,
373 /* methods */
374 VBoxServiceControlPreInit,
375 VBoxServiceControlOption,
376 VBoxServiceControlInit,
377 VBoxServiceControlWorker,
378 VBoxServiceControlStop,
379 VBoxServiceControlTerm
380};
381
Note: See TracBrowser for help on using the repository browser.

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