VirtualBox

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

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

VBoxService: Don't terminate if some HGCM host service is not available on the host.

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