VirtualBox

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

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

Guest Control: Remove unused Std* arguments.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 13.3 KB
Line 
1/* $Id: VBoxServiceControl.cpp 29528 2010-05-17 11:11:03Z 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 != 11)
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 /* Credentials */
139 szUser, sizeof(szUser),
140 szPassword, sizeof(szPassword),
141 /* Timelimit */
142 &uTimeLimitMS);
143 if (RT_FAILURE(rc))
144 {
145 VBoxServiceError("Control: Failed to retrieve exec start command! Error: %Rrc\n", rc);
146 }
147 else
148 {
149 rc = VBoxServiceControlExecProcess(uContextID, szCmd, uFlags, szArgs, uNumArgs,
150 szEnv, cbEnv, uNumEnvVars,
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);
245 if (RT_FAILURE(rc))
246 {
247 if (rc == VERR_TOO_MUCH_DATA)
248 {
249 VBoxServiceVerbose(4, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\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_HOST_CANCEL_WAIT:
262 VBoxServiceVerbose(3, "Control: Host asked us to quit ...\n");
263 break;
264
265 case GETHOSTMSG_EXEC_START_PROCESS:
266 rc = VBoxServiceControlHandleCmdStartProcess(g_GuestControlSvcClientID, uNumParms);
267 break;
268
269 case GETHOSTMSG_EXEC_GET_OUTPUT:
270 rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, uNumParms);
271 break;
272
273 default:
274 VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
275 /* Don't terminate here; just wait for the next message. */
276 break;
277 }
278
279 if (RT_FAILURE(rc))
280 VBoxServiceVerbose(3, "Control: Message was processed with rc=%Rrc\n", rc);
281 }
282
283 /* Do we need to shutdown? */
284 if ( *pfShutdown
285 || uMsg == GETHOSTMSG_EXEC_HOST_CANCEL_WAIT)
286 {
287 rc = VINF_SUCCESS;
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 VBoxServiceVerbose(3, "Control: Stopping ...\n");
305
306 /** @todo Later, figure what to do if we're in RTProcWait(). it's a very
307 * annoying call since doesn't support timeouts in the posix world. */
308 RTSemEventMultiSignal(g_hControlEvent);
309
310 /*
311 * Ask the host service to cancel all pending requests so that we can
312 * shutdown properly here.
313 */
314 if (g_GuestControlSvcClientID)
315 {
316 int rc = VbglR3GuestCtrlCancelPendingWaits(g_GuestControlSvcClientID);
317 if (RT_FAILURE(rc))
318 VBoxServiceError("Control: Cancelling pending waits failed; rc=%Rrc\n", rc);
319 }
320}
321
322
323/** @copydoc VBOXSERVICE::pfnTerm */
324static DECLCALLBACK(void) VBoxServiceControlTerm(void)
325{
326 VBoxServiceVerbose(3, "Control: Terminating ...\n");
327
328 /* Signal all threads that we want to shutdown. */
329 PVBOXSERVICECTRLTHREAD pNode;
330 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
331 ASMAtomicXchgBool(&pNode->fShutdown, true);
332
333 /* Wait for threads to shutdown. */
334 RTListForEach(&g_GuestControlExecThreads, pNode, VBOXSERVICECTRLTHREAD, Node)
335 {
336 if (pNode->Thread != NIL_RTTHREAD)
337 {
338 /* Wait a bit ... */
339 int rc2 = RTThreadWait(pNode->Thread, 30 * 1000 /* Wait 30 seconds max. */, NULL);
340 if (RT_FAILURE(rc2))
341 VBoxServiceError("Control: Thread failed to stop; rc2=%Rrc\n", rc2);
342 }
343
344 /* Destroy thread specific data. */
345 switch (pNode->enmType)
346 {
347 case VBoxServiceCtrlThreadDataExec:
348 VBoxServiceControlExecDestroyThreadData((PVBOXSERVICECTRLTHREADDATAEXEC)pNode->pvData);
349 break;
350
351 default:
352 break;
353 }
354 }
355
356 /* Finally destroy thread list. */
357 pNode = RTListNodeGetFirst(&g_GuestControlExecThreads, VBOXSERVICECTRLTHREAD, Node);
358 while (pNode)
359 {
360 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pNode->Node, VBOXSERVICECTRLTHREAD, Node);
361
362 RTListNodeRemove(&pNode->Node);
363 RTMemFree(pNode);
364
365 if (pNext && RTListNodeIsLast(&g_GuestControlExecThreads, &pNext->Node))
366 break;
367
368 pNode = pNext;
369 }
370
371 VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
372 g_GuestControlSvcClientID = 0;
373
374 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
375 {
376 RTSemEventMultiDestroy(g_hControlEvent);
377 g_hControlEvent = NIL_RTSEMEVENTMULTI;
378 }
379}
380
381
382/**
383 * The 'vminfo' service description.
384 */
385VBOXSERVICE g_Control =
386{
387 /* pszName. */
388 "control",
389 /* pszDescription. */
390 "Host-driven Guest Control",
391 /* pszUsage. */
392 "[--control-interval <ms>]"
393 ,
394 /* pszOptions. */
395 " --control-interval Specifies the interval at which to check for\n"
396 " new control commands. The default is 1000 ms.\n"
397 ,
398 /* methods */
399 VBoxServiceControlPreInit,
400 VBoxServiceControlOption,
401 VBoxServiceControlInit,
402 VBoxServiceControlWorker,
403 VBoxServiceControlStop,
404 VBoxServiceControlTerm
405};
406
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