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