VirtualBox

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

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

VBoxService/GuestCtrl: More message filtering for guest session + process threads; message processing not needed there, logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 17.4 KB
Line 
1/* $Id: VBoxServiceControl.cpp 47622 2013-08-08 21:10:54Z 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#ifdef DEBUG
228 VBoxServiceVerbose(3, "Msg=%RU32 (%RU32 parms) retrieved\n", uMsg, cParms);
229#endif
230 /* Set number of parameters for current host context. */
231 ctxHost.uNumParms = cParms;
232
233 /* Check for VM session change. */
234 uint64_t idNewSession = g_idControlSession;
235 int rc2 = VbglR3GetSessionId(&idNewSession);
236 if ( RT_SUCCESS(rc2)
237 && (idNewSession != g_idControlSession))
238 {
239 VBoxServiceVerbose(1, "The VM session ID changed\n");
240 g_idControlSession = idNewSession;
241
242 /* Close all opened guest sessions -- all context IDs, sessions etc.
243 * are now invalid. */
244 rc2 = GstCntlSessionClose(&g_Session);
245 AssertRC(rc2);
246 }
247
248 switch (uMsg)
249 {
250 case HOST_CANCEL_PENDING_WAITS:
251 VBoxServiceVerbose(1, "We were asked to quit ...\n");
252 break;
253
254 case HOST_SESSION_CREATE:
255 rc = gstcntlHandleSessionOpen(&ctxHost);
256 break;
257
258 case HOST_SESSION_CLOSE:
259 rc = gstcntlHandleSessionClose(&ctxHost);
260 break;
261
262 default:
263 {
264 /*
265 * Protocol v1 did not have support for (dedicated)
266 * guest sessions, so all actions need to be performed
267 * under behalf of VBoxService's main executable.
268 *
269 * The global session object then acts as a host for all
270 * started guest processes which bring all their
271 * credentials with them with the actual execution call.
272 */
273 if (ctxHost.uProtocol == 1)
274 {
275 rc = GstCntlSessionHandler(&g_Session, uMsg, &ctxHost,
276 pvScratchBuf, cbScratchBuf, pfShutdown);
277 }
278 else
279 {
280 /*
281 * ... on newer protocols handling all other commands is
282 * up to the guest session fork of VBoxService, so just
283 * skip all not wanted messages here.
284 */
285 rc = VbglR3GuestCtrlMsgSkip(g_uControlSvcClientID);
286 VBoxServiceVerbose(3, "Skipping uMsg=%RU32, cParms=%RU32, rc=%Rrc\n",
287 uMsg, cParms, rc);
288 }
289 break;
290 }
291 }
292 }
293
294 /* Do we need to shutdown? */
295 if ( *pfShutdown
296 || (RT_SUCCESS(rc) && uMsg == HOST_CANCEL_PENDING_WAITS))
297 {
298 break;
299 }
300
301 /* Let's sleep for a bit and let others run ... */
302 RTThreadYield();
303 }
304
305 VBoxServiceVerbose(0, "Guest control service stopped\n");
306
307 /* Delete scratch buffer. */
308 if (pvScratchBuf)
309 RTMemFree(pvScratchBuf);
310
311 VBoxServiceVerbose(0, "Guest control worker returned with rc=%Rrc\n", rc);
312 return rc;
313}
314
315
316static int gstcntlHandleSessionOpen(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
317{
318 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
319
320 VBOXSERVICECTRLSESSIONSTARTUPINFO ssInfo = { 0 };
321 int rc = VbglR3GuestCtrlSessionGetOpen(pHostCtx,
322 &ssInfo.uProtocol,
323 ssInfo.szUser, sizeof(ssInfo.szUser),
324 ssInfo.szPassword, sizeof(ssInfo.szPassword),
325 ssInfo.szDomain, sizeof(ssInfo.szDomain),
326 &ssInfo.uFlags, &ssInfo.uSessionID);
327 if (RT_SUCCESS(rc))
328 {
329 /* The session open call has the protocol version the host
330 * wants to use. So update the current protocol version with the one the
331 * host wants to use in subsequent calls. */
332 pHostCtx->uProtocol = ssInfo.uProtocol;
333 VBoxServiceVerbose(3, "Client ID=%RU32 now is using protocol %RU32\n",
334 pHostCtx->uClientID, pHostCtx->uProtocol);
335
336 rc = GstCntlSessionThreadCreate(&g_lstControlSessionThreads,
337 &ssInfo, NULL /* ppSessionThread */);
338 }
339
340 if (RT_FAILURE(rc))
341 {
342 /* Report back on failure. On success this will be done
343 * by the forked session thread. */
344 int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx,
345 GUEST_SESSION_NOTIFYTYPE_ERROR, rc /* uint32_t vs. int */);
346 if (RT_FAILURE(rc2))
347 VBoxServiceError("Reporting session error status on open failed with rc=%Rrc\n", rc2);
348 }
349
350 VBoxServiceVerbose(3, "Opening a new guest session returned rc=%Rrc\n", rc);
351 return rc;
352}
353
354
355static int gstcntlHandleSessionClose(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
356{
357 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
358
359 uint32_t uSessionID, uFlags;
360 int rc = VbglR3GuestCtrlSessionGetClose(pHostCtx, &uFlags, &uSessionID);
361 if (RT_SUCCESS(rc))
362 {
363 rc = VERR_NOT_FOUND;
364
365 PVBOXSERVICECTRLSESSIONTHREAD pThread;
366 RTListForEach(&g_lstControlSessionThreads, pThread, VBOXSERVICECTRLSESSIONTHREAD, Node)
367 {
368 if (pThread->StartupInfo.uSessionID == uSessionID)
369 {
370 rc = GstCntlSessionThreadDestroy(pThread, uFlags);
371 break;
372 }
373 }
374#if 0
375 if (RT_FAILURE(rc))
376 {
377 /* Report back on failure. On success this will be done
378 * by the forked session thread. */
379 int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx,
380 GUEST_SESSION_NOTIFYTYPE_ERROR, rc);
381 if (RT_FAILURE(rc2))
382 {
383 VBoxServiceError("Reporting session error status on close failed with rc=%Rrc\n", rc2);
384 if (RT_SUCCESS(rc))
385 rc = rc2;
386 }
387 }
388#endif
389 VBoxServiceVerbose(2, "Closing guest session %RU32 returned rc=%Rrc\n",
390 uSessionID, rc);
391 }
392 else
393 VBoxServiceError("Closing guest session failed with rc=%Rrc\n", rc);
394
395 return rc;
396}
397
398
399/** @copydoc VBOXSERVICE::pfnStop */
400static DECLCALLBACK(void) VBoxServiceControlStop(void)
401{
402 VBoxServiceVerbose(3, "Stopping ...\n");
403
404 /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
405 * annoying call since doesn't support timeouts in the posix world. */
406 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
407 RTSemEventMultiSignal(g_hControlEvent);
408
409 /*
410 * Ask the host service to cancel all pending requests so that we can
411 * shutdown properly here.
412 */
413 if (g_uControlSvcClientID)
414 {
415 VBoxServiceVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
416 g_uControlSvcClientID);
417
418 int rc = VbglR3GuestCtrlCancelPendingWaits(g_uControlSvcClientID);
419 if (RT_FAILURE(rc))
420 VBoxServiceError("Cancelling pending waits failed; rc=%Rrc\n", rc);
421 }
422}
423
424
425/**
426 * Destroys all guest process threads which are still active.
427 */
428static void VBoxServiceControlShutdown(void)
429{
430 VBoxServiceVerbose(2, "Shutting down ...\n");
431
432 int rc2 = GstCntlSessionThreadDestroyAll(&g_lstControlSessionThreads,
433 0 /* Flags */);
434 if (RT_FAILURE(rc2))
435 VBoxServiceError("Closing session threads failed with rc=%Rrc\n", rc2);
436
437 rc2 = GstCntlSessionClose(&g_Session);
438 if (RT_FAILURE(rc2))
439 VBoxServiceError("Closing session failed with rc=%Rrc\n", rc2);
440
441 VBoxServiceVerbose(2, "Shutting down complete\n");
442}
443
444
445/** @copydoc VBOXSERVICE::pfnTerm */
446static DECLCALLBACK(void) VBoxServiceControlTerm(void)
447{
448 VBoxServiceVerbose(3, "Terminating ...\n");
449
450 VBoxServiceControlShutdown();
451
452 VBoxServiceVerbose(3, "Disconnecting client ID=%u ...\n",
453 g_uControlSvcClientID);
454 VbglR3GuestCtrlDisconnect(g_uControlSvcClientID);
455 g_uControlSvcClientID = 0;
456
457 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
458 {
459 RTSemEventMultiDestroy(g_hControlEvent);
460 g_hControlEvent = NIL_RTSEMEVENTMULTI;
461 }
462}
463
464
465/**
466 * The 'vminfo' service description.
467 */
468VBOXSERVICE g_Control =
469{
470 /* pszName. */
471 "control",
472 /* pszDescription. */
473 "Host-driven Guest Control",
474 /* pszUsage. */
475#ifdef DEBUG
476 " [--control-dump-stderr] [--control-dump-stdout]\n"
477#endif
478 " [--control-interval <ms>]"
479 ,
480 /* pszOptions. */
481#ifdef DEBUG
482 " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
483 " temporary directory.\n"
484 " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
485 " temporary directory.\n"
486#endif
487 " --control-interval Specifies the interval at which to check for\n"
488 " new control commands. The default is 1000 ms.\n"
489 ,
490 /* methods */
491 VBoxServiceControlPreInit,
492 VBoxServiceControlOption,
493 VBoxServiceControlInit,
494 VBoxServiceControlWorker,
495 VBoxServiceControlStop,
496 VBoxServiceControlTerm
497};
498
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