VirtualBox

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

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

VBoxService,VBox/err.h: Use a dedicated status code for this, VERR_NOT_IMPLEMENTED and VERR_NOT_SUPPORTED are not suitable. Disable ballooning if either VbglR3MemBalloonRefresh or VBoxServiceBalloonSetUser fails instead of just checking the first. Don't display an error message on non-Windows hosts when ballooning is unavailbe (status code mixup).

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 12.9 KB
Line 
1/* $Id: VBoxServiceControl.cpp 29345 2010-05-11 12:22:48Z 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
33using namespace guestControl;
34
35/*******************************************************************************
36* Global Variables *
37*******************************************************************************/
38/** The control interval (millseconds). */
39uint32_t g_ControlInterval = 0;
40/** The semaphore we're blocking on. */
41static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
42/** The guest property service client ID. */
43static uint32_t g_GuestControlSvcClientID = 0;
44/** List of spawned processes */
45RTLISTNODE g_GuestControlExecThreads;
46
47
48/** @copydoc VBOXSERVICE::pfnPreInit */
49static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
50{
51 return VINF_SUCCESS;
52}
53
54
55/** @copydoc VBOXSERVICE::pfnOption */
56static 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 */
69static 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
107static 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 szStdIn[_1K];
118 char szStdOut[_1K];
119 char szStdErr[_1K];
120 char szUser[128];
121 char szPassword[128];
122 uint32_t uTimeLimitMS;
123
124 if (uNumParms != 14)
125 return VERR_INVALID_PARAMETER;
126
127 int rc = VbglR3GuestCtrlExecGetHostCmd(u32ClientId,
128 uNumParms,
129 &uContextID,
130 /* Command */
131 szCmd, sizeof(szCmd),
132 /* Flags */
133 &uFlags,
134 /* Arguments */
135 szArgs, sizeof(szArgs), &uNumArgs,
136 /* Environment */
137 szEnv, &cbEnv, &uNumEnvVars,
138 /* Pipes */
139 szStdIn, sizeof(szStdIn),
140 szStdOut, sizeof(szStdOut),
141 szStdErr, sizeof(szStdErr),
142 /* Credentials */
143 szUser, sizeof(szUser),
144 szPassword, sizeof(szPassword),
145 /* Timelimit */
146 &uTimeLimitMS);
147 if (RT_FAILURE(rc))
148 {
149 VBoxServiceError("Control: Failed to retrieve exec start command! Error: %Rrc\n", rc);
150 }
151 else
152 {
153 rc = VBoxServiceControlExecProcess(uContextID, szCmd, uFlags, szArgs, uNumArgs,
154 szEnv, cbEnv, uNumEnvVars,
155 szStdIn, szStdOut, szStdErr,
156 szUser, szPassword, uTimeLimitMS);
157 }
158
159 VBoxServiceVerbose(3, "Control: VBoxServiceControlHandleCmdStartProcess returned with %Rrc\n", rc);
160 return rc;
161}
162
163
164static int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms)
165{
166 uint32_t uContextID;
167 uint32_t uPID;
168 uint32_t uHandleID;
169 uint32_t uFlags;
170
171 int rc = VbglR3GuestCtrlExecGetHostCmdOutput(u32ClientId, uNumParms,
172 &uContextID, &uPID, &uHandleID, &uFlags);
173 if (RT_FAILURE(rc))
174 {
175 VBoxServiceError("Control: Failed to retrieve exec output command! Error: %Rrc\n", rc);
176 }
177 else
178 {
179 /* Let's have a look if we have a running process with PID = uPID ... */
180 PVBOXSERVICECTRLTHREAD pNode;
181 bool bFound = false;
182 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
183 {
184 if ( pNode->fStarted
185 && pNode->enmType == VBoxServiceCtrlThreadDataExec)
186 {
187 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
188 if (pData && pData->uPID == uPID)
189 {
190 bFound = true;
191 break;
192 }
193 }
194 }
195
196 if (bFound)
197 {
198 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData;
199 AssertPtr(pData);
200
201 uint32_t cbSize = _4K;
202 uint32_t cbRead = cbSize;
203 uint8_t *pBuf = (uint8_t*)RTMemAlloc(cbSize);
204 if (pBuf)
205 {
206 rc = VBoxServiceControlExecReadPipeBufferContent(&pData->stdOut, pBuf, cbSize, &cbRead);
207 if (RT_SUCCESS(rc))
208 {
209 AssertPtr(pBuf);
210 /* cbRead now contains actual size. */
211 rc = VbglR3GuestCtrlExecSendOut(u32ClientId, uContextID, uPID, 0 /* handle ID */, 0 /* flags */,
212 pBuf, cbRead);
213 }
214 RTMemFree(pBuf);
215 }
216 else
217 rc = VERR_NO_MEMORY;
218 }
219 else
220 rc = VERR_NOT_FOUND; /* PID not found! */
221 }
222 VBoxServiceVerbose(3, "Control: VBoxServiceControlHandleCmdGetOutput returned with %Rrc\n", rc);
223 return rc;
224}
225
226
227/** @copydoc VBOXSERVICE::pfnWorker */
228DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
229{
230 /*
231 * Tell the control thread that it can continue
232 * spawning services.
233 */
234 RTThreadUserSignal(RTThreadSelf());
235 Assert(g_GuestControlSvcClientID > 0);
236
237 int rc = VINF_SUCCESS;
238
239 /*
240 * Execution loop.
241 *
242 * @todo
243 */
244 for (;;)
245 {
246 uint32_t uMsg;
247 uint32_t uNumParms;
248 VBoxServiceVerbose(3, "Control: Waiting for host msg ...\n");
249 rc = VbglR3GuestCtrlGetHostMsg(g_GuestControlSvcClientID, &uMsg, &uNumParms, 1000 /* 1s timeout */);
250 if (RT_FAILURE(rc))
251 {
252 if (rc == VERR_TOO_MUCH_DATA)
253 {
254 VBoxServiceVerbose(3, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request ...\n", uNumParms);
255 rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
256 }
257 else
258 VBoxServiceVerbose(3, "Control: Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
259 }
260
261 if (RT_SUCCESS(rc))
262 {
263 VBoxServiceVerbose(3, "Control: Msg=%u (%u parms) retrieved\n", uMsg, uNumParms);
264 switch(uMsg)
265 {
266 case GETHOSTMSG_EXEC_START_PROCESS:
267 rc = VBoxServiceControlHandleCmdStartProcess(g_GuestControlSvcClientID, uNumParms);
268 break;
269
270 case GETHOSTMSG_EXEC_GET_OUTPUT:
271 rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, uNumParms);
272 break;
273
274 default:
275 VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
276 /* Don't terminate here; just wait for the next message. */
277 break;
278 }
279
280 if (RT_FAILURE(rc))
281 VBoxServiceVerbose(3, "Control: Message was processed with rc=%Rrc\n", rc);
282 }
283
284 /* Do we need to shutdown? */
285 if (*pfShutdown)
286 {
287 rc = 0;
288 break;
289 }
290
291 /* Let's sleep for a bit and let others run ... */
292 RTThreadYield();
293 }
294
295 RTSemEventMultiDestroy(g_hControlEvent);
296 g_hControlEvent = NIL_RTSEMEVENTMULTI;
297 return rc;
298}
299
300
301/** @copydoc VBOXSERVICE::pfnStop */
302static DECLCALLBACK(void) VBoxServiceControlStop(void)
303{
304 /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
305 * annoying call since doesn't support timeouts in the posix world. */
306 RTSemEventMultiSignal(g_hControlEvent);
307}
308
309
310/** @copydoc VBOXSERVICE::pfnTerm */
311static DECLCALLBACK(void) VBoxServiceControlTerm(void)
312{
313 /* Signal all threads that we want to shutdown. */
314 PVBOXSERVICECTRLTHREAD pNode;
315 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
316 ASMAtomicXchgBool(&pNode->fShutdown, true);
317
318 /* Wait for threads to shutdown. */
319 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
320 {
321 if (pNode->Thread != NIL_RTTHREAD)
322 {
323 int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
324 if (RT_FAILURE(rc2))
325 VBoxServiceError("Control: Thread failed to stop; rc2=%Rrc\n", rc2);
326 }
327
328 /* Destroy thread specific data. */
329 switch (pNode->enmType)
330 {
331 case VBoxServiceCtrlThreadDataExec:
332 VBoxServiceControlExecDestroyThreadData((PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData);
333 break;
334
335 default:
336 break;
337 }
338 }
339
340 /* Finally destroy thread list. */
341 pNode = RTListNodeGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
342 while (pNode)
343 {
344 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
345
346 RTListNodeRemove(&pNode->Node);
347 RTMemFree(pNode);
348
349 if (pNext && RTListNodeIsLast(&g_GuestControlExecThreads, &pNext->Node))
350 break;
351
352 pNode = pNext;
353 }
354
355 VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
356 g_GuestControlSvcClientID = 0;
357
358 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
359 {
360 RTSemEventMultiDestroy(g_hControlEvent);
361 g_hControlEvent = NIL_RTSEMEVENTMULTI;
362 }
363}
364
365
366/**
367 * The 'vminfo' service description.
368 */
369VBOXSERVICE g_Control =
370{
371 /* pszName. */
372 "control",
373 /* pszDescription. */
374 "Host-driven Guest Control",
375 /* pszUsage. */
376 "[--control-interval <ms>]"
377 ,
378 /* pszOptions. */
379 " --control-interval Specifies the interval at which to check for\n"
380 " new control commands. The default is 1000 ms.\n"
381 ,
382 /* methods */
383 VBoxServiceControlPreInit,
384 VBoxServiceControlOption,
385 VBoxServiceControlInit,
386 VBoxServiceControlWorker,
387 VBoxServiceControlStop,
388 VBoxServiceControlTerm
389};
390
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