VirtualBox

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

Last change on this file since 48429 was 47817, checked in by vboxsync, 11 years ago

GuestCtrl: Update for IGuestFile; some renaming.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.5 KB
Line 
1/* $Id: VBoxServiceControl.cpp 47817 2013-08-16 15:30:15Z vboxsync $ */
2/** @file
3 * VBoxServiceControl - Host-driven Guest Control.
4 */
5
6/*
7 * Copyright (C) 2012-2013 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/env.h>
25#include <iprt/file.h>
26#include <iprt/getopt.h>
27#include <iprt/mem.h>
28#include <iprt/path.h>
29#include <iprt/process.h>
30#include <iprt/semaphore.h>
31#include <iprt/thread.h>
32#include <VBox/VBoxGuestLib.h>
33#include <VBox/HostServices/GuestControlSvc.h>
34#include "VBoxServiceInternal.h"
35#include "VBoxServiceControl.h"
36#include "VBoxServiceUtils.h"
37
38using namespace guestControl;
39
40/*******************************************************************************
41* Global Variables *
42*******************************************************************************/
43/** The control interval (milliseconds). */
44static uint32_t g_uControlIntervalMS = 0;
45/** The semaphore we're blocking our main control thread on. */
46static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
47/** The VM session ID. Changes whenever the VM is restored or reset. */
48static uint64_t g_idControlSession;
49/** The guest control service client ID. */
50static uint32_t g_uControlSvcClientID = 0;
51/** How many started guest processes are kept into memory for supplying
52 * information to the host. Default is 256 processes. If 0 is specified,
53 * the maximum number of processes is unlimited. */
54static uint32_t g_uControlProcsMaxKept = 256;
55/** List of guest control session threads (VBOXSERVICECTRLSESSIONTHREAD).
56 * A guest session thread represents a forked guest session process
57 * of VBoxService. */
58RTLISTANCHOR g_lstControlSessionThreads;
59/** The local session object used for handling all session-related stuff.
60 * When using the legacy guest control protocol (< 2), this session runs
61 * under behalf of the VBoxService main process. On newer protocol versions
62 * each session is a forked version of VBoxService using the appropriate
63 * user credentials for opening a guest session. These forked sessions then
64 * are kept in VBOXSERVICECTRLSESSIONTHREAD structures. */
65VBOXSERVICECTRLSESSION g_Session;
66
67/*******************************************************************************
68* Internal Functions *
69*******************************************************************************/
70static int gstcntlHandleSessionOpen(PVBGLR3GUESTCTRLCMDCTX pHostCtx);
71static int gstcntlHandleSessionClose(PVBGLR3GUESTCTRLCMDCTX pHostCtx);
72static void VBoxServiceControlShutdown(void);
73
74
75/** @copydoc VBOXSERVICE::pfnPreInit */
76static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
77{
78 int rc;
79#ifdef VBOX_WITH_GUEST_PROPS
80 /*
81 * Read the service options from the VM's guest properties.
82 * Note that these options can be overridden by the command line options later.
83 */
84 uint32_t uGuestPropSvcClientID;
85 rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
86 if (RT_FAILURE(rc))
87 {
88 if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
89 {
90 VBoxServiceVerbose(0, "Guest property service is not available, skipping\n");
91 rc = VINF_SUCCESS;
92 }
93 else
94 VBoxServiceError("Failed to connect to the guest property service, rc=%Rrc\n", rc);
95 }
96 else
97 {
98 VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
99 }
100
101 if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
102 rc = VINF_SUCCESS;
103#else
104 /* Nothing to do here yet. */
105 rc = VINF_SUCCESS;
106#endif
107
108 if (RT_SUCCESS(rc))
109 {
110 /* Init session object. */
111 rc = GstCntlSessionInit(&g_Session, 0 /* Flags */);
112 }
113
114 return rc;
115}
116
117
118/** @copydoc VBOXSERVICE::pfnOption */
119static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
120{
121 int rc = -1;
122 if (ppszShort)
123 /* no short options */;
124 else if (!strcmp(argv[*pi], "--control-interval"))
125 rc = VBoxServiceArgUInt32(argc, argv, "", pi,
126 &g_uControlIntervalMS, 1, UINT32_MAX - 1);
127#ifdef DEBUG
128 else if (!strcmp(argv[*pi], "--control-dump-stdout"))
129 {
130 g_Session.uFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT;
131 rc = 0; /* Flag this command as parsed. */
132 }
133 else if (!strcmp(argv[*pi], "--control-dump-stderr"))
134 {
135 g_Session.uFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR;
136 rc = 0; /* Flag this command as parsed. */
137 }
138#endif
139 return rc;
140}
141
142
143/** @copydoc VBOXSERVICE::pfnInit */
144static DECLCALLBACK(int) VBoxServiceControlInit(void)
145{
146 /*
147 * If not specified, find the right interval default.
148 * Then create the event sem to block on.
149 */
150 if (!g_uControlIntervalMS)
151 g_uControlIntervalMS = 1000;
152
153 int rc = RTSemEventMultiCreate(&g_hControlEvent);
154 AssertRCReturn(rc, rc);
155
156 VbglR3GetSessionId(&g_idControlSession);
157 /* The status code is ignored as this information is not available with VBox < 3.2.10. */
158
159 if (RT_SUCCESS(rc))
160 rc = VbglR3GuestCtrlConnect(&g_uControlSvcClientID);
161 if (RT_SUCCESS(rc))
162 {
163 VBoxServiceVerbose(3, "Guest control service client ID=%RU32\n",
164 g_uControlSvcClientID);
165
166 /* Init session thread list. */
167 RTListInit(&g_lstControlSessionThreads);
168 }
169 else
170 {
171 /* If the service was not found, we disable this service without
172 causing VBoxService to fail. */
173 if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
174 {
175 VBoxServiceVerbose(0, "Guest control service is not available\n");
176 rc = VERR_SERVICE_DISABLED;
177 }
178 else
179 VBoxServiceError("Failed to connect to the guest control service! Error: %Rrc\n", rc);
180 RTSemEventMultiDestroy(g_hControlEvent);
181 g_hControlEvent = NIL_RTSEMEVENTMULTI;
182 }
183 return rc;
184}
185
186
187/** @copydoc VBOXSERVICE::pfnWorker */
188DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
189{
190 /*
191 * Tell the control thread that it can continue
192 * spawning services.
193 */
194 RTThreadUserSignal(RTThreadSelf());
195 Assert(g_uControlSvcClientID > 0);
196
197 int rc = VINF_SUCCESS;
198
199 /* Allocate a scratch buffer for commands which also send
200 * payload data with them. */
201 uint32_t cbScratchBuf = _64K; /** @todo Make buffer size configurable via guest properties/argv! */
202 AssertReturn(RT_IS_POWER_OF_TWO(cbScratchBuf), VERR_INVALID_PARAMETER);
203 uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
204 AssertPtrReturn(pvScratchBuf, VERR_NO_MEMORY);
205
206 VBGLR3GUESTCTRLCMDCTX ctxHost = { g_uControlSvcClientID };
207 /* Set default protocol version to 1. */
208 ctxHost.uProtocol = 1;
209
210 for (;;)
211 {
212 VBoxServiceVerbose(3, "Waiting for host msg ...\n");
213 uint32_t uMsg = 0;
214 uint32_t cParms = 0;
215 rc = VbglR3GuestCtrlMsgWaitFor(g_uControlSvcClientID, &uMsg, &cParms);
216 if (rc == VERR_TOO_MUCH_DATA)
217 {
218#ifdef DEBUG
219 VBoxServiceVerbose(4, "Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
220#endif
221 rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
222 }
223 else if (RT_FAILURE(rc))
224 VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
225 if (RT_SUCCESS(rc))
226 {
227 VBoxServiceVerbose(4, "Msg=%RU32 (%RU32 parms) retrieved\n", uMsg, cParms);
228
229 /* Set number of parameters for current host context. */
230 ctxHost.uNumParms = cParms;
231
232 /* Check for VM session change. */
233 uint64_t idNewSession = g_idControlSession;
234 int rc2 = VbglR3GetSessionId(&idNewSession);
235 if ( RT_SUCCESS(rc2)
236 && (idNewSession != g_idControlSession))
237 {
238 VBoxServiceVerbose(1, "The VM session ID changed\n");
239 g_idControlSession = idNewSession;
240
241 /* Close all opened guest sessions -- all context IDs, sessions etc.
242 * are now invalid. */
243 rc2 = GstCntlSessionClose(&g_Session);
244 AssertRC(rc2);
245 }
246
247 switch (uMsg)
248 {
249 case HOST_CANCEL_PENDING_WAITS:
250 VBoxServiceVerbose(1, "We were asked to quit ...\n");
251 break;
252
253 case HOST_SESSION_CREATE:
254 rc = gstcntlHandleSessionOpen(&ctxHost);
255 break;
256
257 case HOST_SESSION_CLOSE:
258 rc = gstcntlHandleSessionClose(&ctxHost);
259 break;
260
261 default:
262 {
263 /*
264 * Protocol v1 did not have support for (dedicated)
265 * guest sessions, so all actions need to be performed
266 * under behalf of VBoxService's main executable.
267 *
268 * The global session object then acts as a host for all
269 * started guest processes which bring all their
270 * credentials with them with the actual execution call.
271 */
272 if (ctxHost.uProtocol == 1)
273 {
274 rc = GstCntlSessionHandler(&g_Session, uMsg, &ctxHost,
275 pvScratchBuf, cbScratchBuf, pfShutdown);
276 }
277 else
278 {
279 /*
280 * ... on newer protocols handling all other commands is
281 * up to the guest session fork of VBoxService, so just
282 * skip all not wanted messages here.
283 */
284 rc = VbglR3GuestCtrlMsgSkip(g_uControlSvcClientID);
285 VBoxServiceVerbose(3, "Skipping uMsg=%RU32, cParms=%RU32, rc=%Rrc\n",
286 uMsg, cParms, rc);
287 }
288 break;
289 }
290 }
291 }
292
293 /* Do we need to shutdown? */
294 if ( *pfShutdown
295 || (RT_SUCCESS(rc) && uMsg == HOST_CANCEL_PENDING_WAITS))
296 {
297 break;
298 }
299
300 /* Let's sleep for a bit and let others run ... */
301 RTThreadYield();
302 }
303
304 VBoxServiceVerbose(0, "Guest control service stopped\n");
305
306 /* Delete scratch buffer. */
307 if (pvScratchBuf)
308 RTMemFree(pvScratchBuf);
309
310 VBoxServiceVerbose(0, "Guest control worker returned with rc=%Rrc\n", rc);
311 return rc;
312}
313
314
315static int gstcntlHandleSessionOpen(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
316{
317 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
318
319 VBOXSERVICECTRLSESSIONSTARTUPINFO ssInfo = { 0 };
320 int rc = VbglR3GuestCtrlSessionGetOpen(pHostCtx,
321 &ssInfo.uProtocol,
322 ssInfo.szUser, sizeof(ssInfo.szUser),
323 ssInfo.szPassword, sizeof(ssInfo.szPassword),
324 ssInfo.szDomain, sizeof(ssInfo.szDomain),
325 &ssInfo.uFlags, &ssInfo.uSessionID);
326 if (RT_SUCCESS(rc))
327 {
328 /* The session open call has the protocol version the host
329 * wants to use. So update the current protocol version with the one the
330 * host wants to use in subsequent calls. */
331 pHostCtx->uProtocol = ssInfo.uProtocol;
332 VBoxServiceVerbose(3, "Client ID=%RU32 now is using protocol %RU32\n",
333 pHostCtx->uClientID, pHostCtx->uProtocol);
334
335 rc = GstCntlSessionThreadCreate(&g_lstControlSessionThreads,
336 &ssInfo, NULL /* ppSessionThread */);
337 }
338
339 if (RT_FAILURE(rc))
340 {
341 /* Report back on failure. On success this will be done
342 * by the forked session thread. */
343 int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx,
344 GUEST_SESSION_NOTIFYTYPE_ERROR, rc /* uint32_t vs. int */);
345 if (RT_FAILURE(rc2))
346 VBoxServiceError("Reporting session error status on open failed with rc=%Rrc\n", rc2);
347 }
348
349 VBoxServiceVerbose(3, "Opening a new guest session returned rc=%Rrc\n", rc);
350 return rc;
351}
352
353
354static int gstcntlHandleSessionClose(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
355{
356 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
357
358 uint32_t uSessionID, uFlags;
359 int rc = VbglR3GuestCtrlSessionGetClose(pHostCtx, &uFlags, &uSessionID);
360 if (RT_SUCCESS(rc))
361 {
362 rc = VERR_NOT_FOUND;
363
364 PVBOXSERVICECTRLSESSIONTHREAD pThread;
365 RTListForEach(&g_lstControlSessionThreads, pThread, VBOXSERVICECTRLSESSIONTHREAD, Node)
366 {
367 if (pThread->StartupInfo.uSessionID == uSessionID)
368 {
369 rc = GstCntlSessionThreadDestroy(pThread, uFlags);
370 break;
371 }
372 }
373#if 0
374 if (RT_FAILURE(rc))
375 {
376 /* Report back on failure. On success this will be done
377 * by the forked session thread. */
378 int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx,
379 GUEST_SESSION_NOTIFYTYPE_ERROR, rc);
380 if (RT_FAILURE(rc2))
381 {
382 VBoxServiceError("Reporting session error status on close failed with rc=%Rrc\n", rc2);
383 if (RT_SUCCESS(rc))
384 rc = rc2;
385 }
386 }
387#endif
388 VBoxServiceVerbose(2, "Closing guest session %RU32 returned rc=%Rrc\n",
389 uSessionID, rc);
390 }
391 else
392 VBoxServiceError("Closing guest session %RU32 failed with rc=%Rrc\n",
393 uSessionID, rc);
394 return rc;
395}
396
397
398/** @copydoc VBOXSERVICE::pfnStop */
399static DECLCALLBACK(void) VBoxServiceControlStop(void)
400{
401 VBoxServiceVerbose(3, "Stopping ...\n");
402
403 /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
404 * annoying call since doesn't support timeouts in the posix world. */
405 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
406 RTSemEventMultiSignal(g_hControlEvent);
407
408 /*
409 * Ask the host service to cancel all pending requests so that we can
410 * shutdown properly here.
411 */
412 if (g_uControlSvcClientID)
413 {
414 VBoxServiceVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
415 g_uControlSvcClientID);
416
417 int rc = VbglR3GuestCtrlCancelPendingWaits(g_uControlSvcClientID);
418 if (RT_FAILURE(rc))
419 VBoxServiceError("Cancelling pending waits failed; rc=%Rrc\n", rc);
420 }
421}
422
423
424/**
425 * Destroys all guest process threads which are still active.
426 */
427static void VBoxServiceControlShutdown(void)
428{
429 VBoxServiceVerbose(2, "Shutting down ...\n");
430
431 int rc2 = GstCntlSessionThreadDestroyAll(&g_lstControlSessionThreads,
432 0 /* Flags */);
433 if (RT_FAILURE(rc2))
434 VBoxServiceError("Closing session threads failed with rc=%Rrc\n", rc2);
435
436 rc2 = GstCntlSessionClose(&g_Session);
437 if (RT_FAILURE(rc2))
438 VBoxServiceError("Closing session failed with rc=%Rrc\n", rc2);
439
440 VBoxServiceVerbose(2, "Shutting down complete\n");
441}
442
443
444/** @copydoc VBOXSERVICE::pfnTerm */
445static DECLCALLBACK(void) VBoxServiceControlTerm(void)
446{
447 VBoxServiceVerbose(3, "Terminating ...\n");
448
449 VBoxServiceControlShutdown();
450
451 VBoxServiceVerbose(3, "Disconnecting client ID=%u ...\n",
452 g_uControlSvcClientID);
453 VbglR3GuestCtrlDisconnect(g_uControlSvcClientID);
454 g_uControlSvcClientID = 0;
455
456 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
457 {
458 RTSemEventMultiDestroy(g_hControlEvent);
459 g_hControlEvent = NIL_RTSEMEVENTMULTI;
460 }
461}
462
463
464/**
465 * The 'vminfo' service description.
466 */
467VBOXSERVICE g_Control =
468{
469 /* pszName. */
470 "control",
471 /* pszDescription. */
472 "Host-driven Guest Control",
473 /* pszUsage. */
474#ifdef DEBUG
475 " [--control-dump-stderr] [--control-dump-stdout]\n"
476#endif
477 " [--control-interval <ms>]"
478 ,
479 /* pszOptions. */
480#ifdef DEBUG
481 " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
482 " temporary directory.\n"
483 " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
484 " temporary directory.\n"
485#endif
486 " --control-interval Specifies the interval at which to check for\n"
487 " new control commands. The default is 1000 ms.\n"
488 ,
489 /* methods */
490 VBoxServiceControlPreInit,
491 VBoxServiceControlOption,
492 VBoxServiceControlInit,
493 VBoxServiceControlWorker,
494 VBoxServiceControlStop,
495 VBoxServiceControlTerm
496};
497
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