1 | /* $Id: VBoxServiceControl.cpp 83405 2020-03-25 12:45:01Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceControl - Host-driven Guest Control.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012-2020 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 | /** @page pg_vgsvc_gstctrl VBoxService - Guest Control
|
---|
19 | *
|
---|
20 | * The Guest Control subservice helps implementing the IGuest APIs.
|
---|
21 | *
|
---|
22 | * The communication between this service (and its children) and IGuest goes
|
---|
23 | * over the HGCM GuestControl service.
|
---|
24 | *
|
---|
25 | * The IGuest APIs provides means to manipulate (control) files, directories,
|
---|
26 | * symbolic links and processes within the guest. Most of these means requires
|
---|
27 | * credentials of a guest OS user to operate, though some restricted ones
|
---|
28 | * operates directly as the VBoxService user (root / system service account).
|
---|
29 | *
|
---|
30 | * The current design is that a subprocess is spawned for handling operations as
|
---|
31 | * a given user. This process is represented as IGuestSession in the API. The
|
---|
32 | * subprocess will be spawned as the given use, giving up the privileges the
|
---|
33 | * parent subservice had.
|
---|
34 | *
|
---|
35 | * It will try handle as many of the operations directly from within the
|
---|
36 | * subprocess, but for more complicated things (or things that haven't yet been
|
---|
37 | * converted), it will spawn a helper process that does the actual work.
|
---|
38 | *
|
---|
39 | * These helpers are the typically modeled on similar unix core utilities, like
|
---|
40 | * mkdir, rm, rmdir, cat and so on. The helper tools can also be launched
|
---|
41 | * directly from VBoxManage by the user by prepending the 'vbox_' prefix to the
|
---|
42 | * unix command.
|
---|
43 | *
|
---|
44 | */
|
---|
45 |
|
---|
46 |
|
---|
47 | /*********************************************************************************************************************************
|
---|
48 | * Header Files *
|
---|
49 | *********************************************************************************************************************************/
|
---|
50 | #include <iprt/asm.h>
|
---|
51 | #include <iprt/assert.h>
|
---|
52 | #include <iprt/env.h>
|
---|
53 | #include <iprt/file.h>
|
---|
54 | #include <iprt/getopt.h>
|
---|
55 | #include <iprt/mem.h>
|
---|
56 | #include <iprt/path.h>
|
---|
57 | #include <iprt/process.h>
|
---|
58 | #include <iprt/semaphore.h>
|
---|
59 | #include <iprt/thread.h>
|
---|
60 | #include <VBox/err.h>
|
---|
61 | #include <VBox/VBoxGuestLib.h>
|
---|
62 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
63 | #include "VBoxServiceInternal.h"
|
---|
64 | #include "VBoxServiceControl.h"
|
---|
65 | #include "VBoxServiceUtils.h"
|
---|
66 |
|
---|
67 | using namespace guestControl;
|
---|
68 |
|
---|
69 |
|
---|
70 | /*********************************************************************************************************************************
|
---|
71 | * Global Variables *
|
---|
72 | *********************************************************************************************************************************/
|
---|
73 | /** The control interval (milliseconds). */
|
---|
74 | static uint32_t g_msControlInterval = 0;
|
---|
75 | /** The semaphore we're blocking our main control thread on. */
|
---|
76 | static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
77 | /** The VM session ID. Changes whenever the VM is restored or reset. */
|
---|
78 | static uint64_t g_idControlSession;
|
---|
79 | /** The guest control service client ID. */
|
---|
80 | uint32_t g_idControlSvcClient = 0;
|
---|
81 | /** VBOX_GUESTCTRL_HF_XXX */
|
---|
82 | uint64_t g_fControlHostFeatures0 = 0;
|
---|
83 | #if 0 /** @todo process limit */
|
---|
84 | /** How many started guest processes are kept into memory for supplying
|
---|
85 | * information to the host. Default is 256 processes. If 0 is specified,
|
---|
86 | * the maximum number of processes is unlimited. */
|
---|
87 | static uint32_t g_uControlProcsMaxKept = 256;
|
---|
88 | #endif
|
---|
89 | /** List of guest control session threads (VBOXSERVICECTRLSESSIONTHREAD).
|
---|
90 | * A guest session thread represents a forked guest session process
|
---|
91 | * of VBoxService. */
|
---|
92 | RTLISTANCHOR g_lstControlSessionThreads;
|
---|
93 | /** The local session object used for handling all session-related stuff.
|
---|
94 | * When using the legacy guest control protocol (< 2), this session runs
|
---|
95 | * under behalf of the VBoxService main process. On newer protocol versions
|
---|
96 | * each session is a forked version of VBoxService using the appropriate
|
---|
97 | * user credentials for opening a guest session. These forked sessions then
|
---|
98 | * are kept in VBOXSERVICECTRLSESSIONTHREAD structures. */
|
---|
99 | VBOXSERVICECTRLSESSION g_Session;
|
---|
100 | /** Copy of VbglR3GuestCtrlSupportsOptimizations().*/
|
---|
101 | bool g_fControlSupportsOptimizations = true;
|
---|
102 |
|
---|
103 |
|
---|
104 | /*********************************************************************************************************************************
|
---|
105 | * Internal Functions *
|
---|
106 | *********************************************************************************************************************************/
|
---|
107 | static int vgsvcGstCtrlHandleSessionOpen(PVBGLR3GUESTCTRLCMDCTX pHostCtx);
|
---|
108 | static int vgsvcGstCtrlHandleSessionClose(PVBGLR3GUESTCTRLCMDCTX pHostCtx);
|
---|
109 | static void vgsvcGstCtrlShutdown(void);
|
---|
110 |
|
---|
111 |
|
---|
112 | /**
|
---|
113 | * @interface_method_impl{VBOXSERVICE,pfnPreInit}
|
---|
114 | */
|
---|
115 | static DECLCALLBACK(int) vgsvcGstCtrlPreInit(void)
|
---|
116 | {
|
---|
117 | int rc;
|
---|
118 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
119 | /*
|
---|
120 | * Read the service options from the VM's guest properties.
|
---|
121 | * Note that these options can be overridden by the command line options later.
|
---|
122 | */
|
---|
123 | uint32_t uGuestPropSvcClientID;
|
---|
124 | rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
125 | if (RT_FAILURE(rc))
|
---|
126 | {
|
---|
127 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
128 | {
|
---|
129 | VGSvcVerbose(0, "Guest property service is not available, skipping\n");
|
---|
130 | rc = VINF_SUCCESS;
|
---|
131 | }
|
---|
132 | else
|
---|
133 | VGSvcError("Failed to connect to the guest property service, rc=%Rrc\n", rc);
|
---|
134 | }
|
---|
135 | else
|
---|
136 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
137 |
|
---|
138 | if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
|
---|
139 | rc = VINF_SUCCESS;
|
---|
140 | #else
|
---|
141 | /* Nothing to do here yet. */
|
---|
142 | rc = VINF_SUCCESS;
|
---|
143 | #endif
|
---|
144 |
|
---|
145 | if (RT_SUCCESS(rc))
|
---|
146 | {
|
---|
147 | /* Init session object. */
|
---|
148 | rc = VGSvcGstCtrlSessionInit(&g_Session, 0 /* Flags */);
|
---|
149 | }
|
---|
150 |
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /**
|
---|
156 | * @interface_method_impl{VBOXSERVICE,pfnOption}
|
---|
157 | */
|
---|
158 | static DECLCALLBACK(int) vgsvcGstCtrlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
159 | {
|
---|
160 | int rc = -1;
|
---|
161 | if (ppszShort)
|
---|
162 | /* no short options */;
|
---|
163 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
164 | rc = VGSvcArgUInt32(argc, argv, "", pi,
|
---|
165 | &g_msControlInterval, 1, UINT32_MAX - 1);
|
---|
166 | #ifdef DEBUG
|
---|
167 | else if (!strcmp(argv[*pi], "--control-dump-stdout"))
|
---|
168 | {
|
---|
169 | g_Session.fFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT;
|
---|
170 | rc = 0; /* Flag this command as parsed. */
|
---|
171 | }
|
---|
172 | else if (!strcmp(argv[*pi], "--control-dump-stderr"))
|
---|
173 | {
|
---|
174 | g_Session.fFlags |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR;
|
---|
175 | rc = 0; /* Flag this command as parsed. */
|
---|
176 | }
|
---|
177 | #endif
|
---|
178 | return rc;
|
---|
179 | }
|
---|
180 |
|
---|
181 |
|
---|
182 | /**
|
---|
183 | * @interface_method_impl{VBOXSERVICE,pfnInit}
|
---|
184 | */
|
---|
185 | static DECLCALLBACK(int) vgsvcGstCtrlInit(void)
|
---|
186 | {
|
---|
187 | /*
|
---|
188 | * If not specified, find the right interval default.
|
---|
189 | * Then create the event sem to block on.
|
---|
190 | */
|
---|
191 | if (!g_msControlInterval)
|
---|
192 | g_msControlInterval = 1000;
|
---|
193 |
|
---|
194 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
195 | AssertRCReturn(rc, rc);
|
---|
196 |
|
---|
197 | VbglR3GetSessionId(&g_idControlSession); /* The status code is ignored as this information is not available with VBox < 3.2.10. */
|
---|
198 |
|
---|
199 | RTListInit(&g_lstControlSessionThreads);
|
---|
200 |
|
---|
201 | /*
|
---|
202 | * Try connect to the host service and tell it we want to be master (if supported).
|
---|
203 | */
|
---|
204 | rc = VbglR3GuestCtrlConnect(&g_idControlSvcClient);
|
---|
205 | if (RT_SUCCESS(rc))
|
---|
206 | {
|
---|
207 | g_fControlSupportsOptimizations = VbglR3GuestCtrlSupportsOptimizations(g_idControlSvcClient);
|
---|
208 | if (g_fControlSupportsOptimizations)
|
---|
209 | rc = VbglR3GuestCtrlMakeMeMaster(g_idControlSvcClient);
|
---|
210 | if (RT_SUCCESS(rc))
|
---|
211 | {
|
---|
212 | VGSvcVerbose(3, "Guest control service client ID=%RU32%s\n",
|
---|
213 | g_idControlSvcClient, g_fControlSupportsOptimizations ? " w/ optimizations" : "");
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Report features to the host.
|
---|
217 | */
|
---|
218 | const uint64_t fGuestFeatures = VBOX_GUESTCTRL_GF_0_SET_SIZE
|
---|
219 | | VBOX_GUESTCTRL_GF_0_PROCESS_ARGV0;
|
---|
220 |
|
---|
221 | rc = VbglR3GuestCtrlReportFeatures(g_idControlSvcClient, fGuestFeatures, &g_fControlHostFeatures0);
|
---|
222 | if (RT_SUCCESS(rc))
|
---|
223 | VGSvcVerbose(3, "Host features: %#RX64\n", g_fControlHostFeatures0);
|
---|
224 | else
|
---|
225 | VGSvcVerbose(1, "Warning! Feature reporing failed: %Rrc\n", rc);
|
---|
226 |
|
---|
227 | return VINF_SUCCESS;
|
---|
228 | }
|
---|
229 | VGSvcError("Failed to become guest control master: %Rrc\n", rc);
|
---|
230 | VbglR3GuestCtrlDisconnect(g_idControlSvcClient);
|
---|
231 | }
|
---|
232 | else
|
---|
233 | {
|
---|
234 | /* If the service was not found, we disable this service without
|
---|
235 | causing VBoxService to fail. */
|
---|
236 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
237 | {
|
---|
238 | VGSvcVerbose(0, "Guest control service is not available\n");
|
---|
239 | rc = VERR_SERVICE_DISABLED;
|
---|
240 | }
|
---|
241 | else
|
---|
242 | VGSvcError("Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
243 | }
|
---|
244 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
245 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
246 | g_idControlSvcClient = 0;
|
---|
247 | return rc;
|
---|
248 | }
|
---|
249 |
|
---|
250 |
|
---|
251 | /**
|
---|
252 | * @interface_method_impl{VBOXSERVICE,pfnWorker}
|
---|
253 | */
|
---|
254 | static DECLCALLBACK(int) vgsvcGstCtrlWorker(bool volatile *pfShutdown)
|
---|
255 | {
|
---|
256 | /*
|
---|
257 | * Tell the control thread that it can continue spawning services.
|
---|
258 | */
|
---|
259 | RTThreadUserSignal(RTThreadSelf());
|
---|
260 | Assert(g_idControlSvcClient > 0);
|
---|
261 |
|
---|
262 | /* Allocate a scratch buffer for messages which also send
|
---|
263 | * payload data with them. */
|
---|
264 | uint32_t cbScratchBuf = _64K; /** @todo Make buffer size configurable via guest properties/argv! */
|
---|
265 | AssertReturn(RT_IS_POWER_OF_TWO(cbScratchBuf), VERR_INVALID_PARAMETER);
|
---|
266 | uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
|
---|
267 | AssertReturn(pvScratchBuf, VERR_NO_MEMORY);
|
---|
268 |
|
---|
269 | int rc = VINF_SUCCESS; /* (shut up compiler warnings) */
|
---|
270 | int cRetrievalFailed = 0; /* Number of failed message retrievals in a row. */
|
---|
271 | while (!*pfShutdown)
|
---|
272 | {
|
---|
273 | VGSvcVerbose(3, "GstCtrl: Waiting for host msg ...\n");
|
---|
274 | VBGLR3GUESTCTRLCMDCTX ctxHost = { g_idControlSvcClient, 0 /*idContext*/, 2 /*uProtocol*/, 0 /*cParms*/ };
|
---|
275 | uint32_t idMsg = 0;
|
---|
276 | rc = VbglR3GuestCtrlMsgPeekWait(g_idControlSvcClient, &idMsg, &ctxHost.uNumParms, &g_idControlSession);
|
---|
277 | if (RT_SUCCESS(rc))
|
---|
278 | {
|
---|
279 | cRetrievalFailed = 0; /* Reset failed retrieval count. */
|
---|
280 | VGSvcVerbose(4, "idMsg=%RU32 (%s) (%RU32 parms) retrieved\n",
|
---|
281 | idMsg, GstCtrlHostMsgtoStr((eHostMsg)idMsg), ctxHost.uNumParms);
|
---|
282 |
|
---|
283 | /*
|
---|
284 | * Handle the host message.
|
---|
285 | */
|
---|
286 | switch (idMsg)
|
---|
287 | {
|
---|
288 | case HOST_MSG_CANCEL_PENDING_WAITS:
|
---|
289 | VGSvcVerbose(1, "We were asked to quit ...\n");
|
---|
290 | break;
|
---|
291 |
|
---|
292 | case HOST_MSG_SESSION_CREATE:
|
---|
293 | rc = vgsvcGstCtrlHandleSessionOpen(&ctxHost);
|
---|
294 | break;
|
---|
295 |
|
---|
296 | /* This message is also sent to the child session process (by the host). */
|
---|
297 | case HOST_MSG_SESSION_CLOSE:
|
---|
298 | rc = vgsvcGstCtrlHandleSessionClose(&ctxHost);
|
---|
299 | break;
|
---|
300 |
|
---|
301 | default:
|
---|
302 | if (VbglR3GuestCtrlSupportsOptimizations(g_idControlSvcClient))
|
---|
303 | {
|
---|
304 | rc = VbglR3GuestCtrlMsgSkip(g_idControlSvcClient, VERR_NOT_SUPPORTED, idMsg);
|
---|
305 | VGSvcVerbose(1, "Skipped unexpected message idMsg=%RU32 (%s), cParms=%RU32 (rc=%Rrc)\n",
|
---|
306 | idMsg, GstCtrlHostMsgtoStr((eHostMsg)idMsg), ctxHost.uNumParms, rc);
|
---|
307 | }
|
---|
308 | else
|
---|
309 | {
|
---|
310 | rc = VbglR3GuestCtrlMsgSkipOld(g_idControlSvcClient);
|
---|
311 | VGSvcVerbose(3, "Skipped idMsg=%RU32, cParms=%RU32, rc=%Rrc\n", idMsg, ctxHost.uNumParms, rc);
|
---|
312 | }
|
---|
313 | break;
|
---|
314 | }
|
---|
315 |
|
---|
316 | /* Do we need to shutdown? */
|
---|
317 | if (idMsg == HOST_MSG_CANCEL_PENDING_WAITS)
|
---|
318 | break;
|
---|
319 |
|
---|
320 | /* Let's sleep for a bit and let others run ... */
|
---|
321 | RTThreadYield();
|
---|
322 | }
|
---|
323 | /*
|
---|
324 | * Handle restore notification from host. All the context IDs (sessions,
|
---|
325 | * files, proceses, etc) are invalidated by a VM restore and must be closed.
|
---|
326 | */
|
---|
327 | else if (rc == VERR_VM_RESTORED)
|
---|
328 | {
|
---|
329 | VGSvcVerbose(1, "The VM session ID changed (i.e. restored).\n");
|
---|
330 | int rc2 = VGSvcGstCtrlSessionClose(&g_Session);
|
---|
331 | AssertRC(rc2);
|
---|
332 | }
|
---|
333 | else
|
---|
334 | {
|
---|
335 | /* Note: VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
|
---|
336 | /** @todo r=bird: Above comment makes no sense. How can you get a timeout in a blocking HGCM call? */
|
---|
337 | VGSvcError("GstCtrl: Getting host message failed with %Rrc\n", rc);
|
---|
338 |
|
---|
339 | /* Check for VM session change. */
|
---|
340 | /** @todo We don't need to check the host here. */
|
---|
341 | uint64_t idNewSession = g_idControlSession;
|
---|
342 | int rc2 = VbglR3GetSessionId(&idNewSession);
|
---|
343 | if ( RT_SUCCESS(rc2)
|
---|
344 | && (idNewSession != g_idControlSession))
|
---|
345 | {
|
---|
346 | VGSvcVerbose(1, "GstCtrl: The VM session ID changed\n");
|
---|
347 | g_idControlSession = idNewSession;
|
---|
348 |
|
---|
349 | /* Close all opened guest sessions -- all context IDs, sessions etc.
|
---|
350 | * are now invalid. */
|
---|
351 | rc2 = VGSvcGstCtrlSessionClose(&g_Session);
|
---|
352 | AssertRC(rc2);
|
---|
353 |
|
---|
354 | /* Do a reconnect. */
|
---|
355 | VGSvcVerbose(1, "Reconnecting to HGCM service ...\n");
|
---|
356 | rc2 = VbglR3GuestCtrlConnect(&g_idControlSvcClient);
|
---|
357 | if (RT_SUCCESS(rc2))
|
---|
358 | {
|
---|
359 | VGSvcVerbose(3, "Guest control service client ID=%RU32\n", g_idControlSvcClient);
|
---|
360 | cRetrievalFailed = 0;
|
---|
361 | continue; /* Skip waiting. */
|
---|
362 | }
|
---|
363 | VGSvcError("Unable to re-connect to HGCM service, rc=%Rrc, bailing out\n", rc);
|
---|
364 | break;
|
---|
365 | }
|
---|
366 |
|
---|
367 | if (rc == VERR_INTERRUPTED)
|
---|
368 | RTThreadYield(); /* To be on the safe side... */
|
---|
369 | else if (++cRetrievalFailed <= 16) /** @todo Make this configurable? */
|
---|
370 | RTThreadSleep(1000); /* Wait a bit before retrying. */
|
---|
371 | else
|
---|
372 | {
|
---|
373 | VGSvcError("Too many failed attempts in a row to get next message, bailing out\n");
|
---|
374 | break;
|
---|
375 | }
|
---|
376 | }
|
---|
377 | }
|
---|
378 |
|
---|
379 | VGSvcVerbose(0, "Guest control service stopped\n");
|
---|
380 |
|
---|
381 | /* Delete scratch buffer. */
|
---|
382 | if (pvScratchBuf)
|
---|
383 | RTMemFree(pvScratchBuf);
|
---|
384 |
|
---|
385 | VGSvcVerbose(0, "Guest control worker returned with rc=%Rrc\n", rc);
|
---|
386 | return rc;
|
---|
387 | }
|
---|
388 |
|
---|
389 |
|
---|
390 | static int vgsvcGstCtrlHandleSessionOpen(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
391 | {
|
---|
392 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
393 |
|
---|
394 | /*
|
---|
395 | * Retrieve the message parameters.
|
---|
396 | */
|
---|
397 | VBOXSERVICECTRLSESSIONSTARTUPINFO ssInfo = { 0 };
|
---|
398 | int rc = VbglR3GuestCtrlSessionGetOpen(pHostCtx,
|
---|
399 | &ssInfo.uProtocol,
|
---|
400 | ssInfo.szUser, sizeof(ssInfo.szUser),
|
---|
401 | ssInfo.szPassword, sizeof(ssInfo.szPassword),
|
---|
402 | ssInfo.szDomain, sizeof(ssInfo.szDomain),
|
---|
403 | &ssInfo.fFlags, &ssInfo.uSessionID);
|
---|
404 | if (RT_SUCCESS(rc))
|
---|
405 | {
|
---|
406 | /*
|
---|
407 | * Flat out refuse to work with protocol v1 hosts.
|
---|
408 | */
|
---|
409 | if (ssInfo.uProtocol == 2)
|
---|
410 | {
|
---|
411 | pHostCtx->uProtocol = ssInfo.uProtocol;
|
---|
412 | VGSvcVerbose(3, "Client ID=%RU32 now is using protocol %RU32\n", pHostCtx->uClientID, pHostCtx->uProtocol);
|
---|
413 |
|
---|
414 | /** @todo Someone explain why this code isn't in this file too? v1 support? */
|
---|
415 | rc = VGSvcGstCtrlSessionThreadCreate(&g_lstControlSessionThreads, &ssInfo, NULL /* ppSessionThread */);
|
---|
416 | /* Report failures to the host (successes are taken care of by the session thread). */
|
---|
417 | }
|
---|
418 | else
|
---|
419 | {
|
---|
420 | VGSvcError("The host wants to use protocol v%u, we only support v2!\n", ssInfo.uProtocol);
|
---|
421 | rc = VERR_VERSION_MISMATCH;
|
---|
422 | }
|
---|
423 | if (RT_FAILURE(rc))
|
---|
424 | {
|
---|
425 | int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx, GUEST_SESSION_NOTIFYTYPE_ERROR, rc);
|
---|
426 | if (RT_FAILURE(rc2))
|
---|
427 | VGSvcError("Reporting session error status on open failed with rc=%Rrc\n", rc2);
|
---|
428 | }
|
---|
429 | }
|
---|
430 | else
|
---|
431 | {
|
---|
432 | VGSvcError("Error fetching parameters for opening guest session: %Rrc\n", rc);
|
---|
433 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
434 | }
|
---|
435 | VGSvcVerbose(3, "Opening a new guest session returned rc=%Rrc\n", rc);
|
---|
436 | return rc;
|
---|
437 | }
|
---|
438 |
|
---|
439 |
|
---|
440 | static int vgsvcGstCtrlHandleSessionClose(PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
441 | {
|
---|
442 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
443 |
|
---|
444 | uint32_t idSession;
|
---|
445 | uint32_t fFlags;
|
---|
446 | int rc = VbglR3GuestCtrlSessionGetClose(pHostCtx, &fFlags, &idSession);
|
---|
447 | if (RT_SUCCESS(rc))
|
---|
448 | {
|
---|
449 | rc = VERR_NOT_FOUND;
|
---|
450 |
|
---|
451 | PVBOXSERVICECTRLSESSIONTHREAD pThread;
|
---|
452 | RTListForEach(&g_lstControlSessionThreads, pThread, VBOXSERVICECTRLSESSIONTHREAD, Node)
|
---|
453 | {
|
---|
454 | if (pThread->StartupInfo.uSessionID == idSession)
|
---|
455 | {
|
---|
456 | rc = VGSvcGstCtrlSessionThreadDestroy(pThread, fFlags);
|
---|
457 | break;
|
---|
458 | }
|
---|
459 | }
|
---|
460 |
|
---|
461 | #if 0 /** @todo A bit of a mess here as this message goes to both to this process (master) and the session process. */
|
---|
462 | if (RT_FAILURE(rc))
|
---|
463 | {
|
---|
464 | /* Report back on failure. On success this will be done
|
---|
465 | * by the forked session thread. */
|
---|
466 | int rc2 = VbglR3GuestCtrlSessionNotify(pHostCtx,
|
---|
467 | GUEST_SESSION_NOTIFYTYPE_ERROR, rc);
|
---|
468 | if (RT_FAILURE(rc2))
|
---|
469 | {
|
---|
470 | VGSvcError("Reporting session error status on close failed with rc=%Rrc\n", rc2);
|
---|
471 | if (RT_SUCCESS(rc))
|
---|
472 | rc = rc2;
|
---|
473 | }
|
---|
474 | }
|
---|
475 | #endif
|
---|
476 | VGSvcVerbose(2, "Closing guest session %RU32 returned rc=%Rrc\n", idSession, rc);
|
---|
477 | }
|
---|
478 | else
|
---|
479 | {
|
---|
480 | VGSvcError("Error fetching parameters for closing guest session: %Rrc\n", rc);
|
---|
481 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
482 | }
|
---|
483 | return rc;
|
---|
484 | }
|
---|
485 |
|
---|
486 |
|
---|
487 | /**
|
---|
488 | * @interface_method_impl{VBOXSERVICE,pfnStop}
|
---|
489 | */
|
---|
490 | static DECLCALLBACK(void) vgsvcGstCtrlStop(void)
|
---|
491 | {
|
---|
492 | VGSvcVerbose(3, "Stopping ...\n");
|
---|
493 |
|
---|
494 | /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
|
---|
495 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
496 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
497 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
498 |
|
---|
499 | /*
|
---|
500 | * Ask the host service to cancel all pending requests for the main
|
---|
501 | * control thread so that we can shutdown properly here.
|
---|
502 | */
|
---|
503 | if (g_idControlSvcClient)
|
---|
504 | {
|
---|
505 | VGSvcVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
|
---|
506 | g_idControlSvcClient);
|
---|
507 |
|
---|
508 | int rc = VbglR3GuestCtrlCancelPendingWaits(g_idControlSvcClient);
|
---|
509 | if (RT_FAILURE(rc))
|
---|
510 | VGSvcError("Cancelling pending waits failed; rc=%Rrc\n", rc);
|
---|
511 | }
|
---|
512 | }
|
---|
513 |
|
---|
514 |
|
---|
515 | /**
|
---|
516 | * Destroys all guest process threads which are still active.
|
---|
517 | */
|
---|
518 | static void vgsvcGstCtrlShutdown(void)
|
---|
519 | {
|
---|
520 | VGSvcVerbose(2, "Shutting down ...\n");
|
---|
521 |
|
---|
522 | int rc2 = VGSvcGstCtrlSessionThreadDestroyAll(&g_lstControlSessionThreads, 0 /* Flags */);
|
---|
523 | if (RT_FAILURE(rc2))
|
---|
524 | VGSvcError("Closing session threads failed with rc=%Rrc\n", rc2);
|
---|
525 |
|
---|
526 | rc2 = VGSvcGstCtrlSessionClose(&g_Session);
|
---|
527 | if (RT_FAILURE(rc2))
|
---|
528 | VGSvcError("Closing session failed with rc=%Rrc\n", rc2);
|
---|
529 |
|
---|
530 | VGSvcVerbose(2, "Shutting down complete\n");
|
---|
531 | }
|
---|
532 |
|
---|
533 |
|
---|
534 | /**
|
---|
535 | * @interface_method_impl{VBOXSERVICE,pfnTerm}
|
---|
536 | */
|
---|
537 | static DECLCALLBACK(void) vgsvcGstCtrlTerm(void)
|
---|
538 | {
|
---|
539 | VGSvcVerbose(3, "Terminating ...\n");
|
---|
540 |
|
---|
541 | vgsvcGstCtrlShutdown();
|
---|
542 |
|
---|
543 | VGSvcVerbose(3, "Disconnecting client ID=%u ...\n", g_idControlSvcClient);
|
---|
544 | VbglR3GuestCtrlDisconnect(g_idControlSvcClient);
|
---|
545 | g_idControlSvcClient = 0;
|
---|
546 |
|
---|
547 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
548 | {
|
---|
549 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
550 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
551 | }
|
---|
552 | }
|
---|
553 |
|
---|
554 |
|
---|
555 | /**
|
---|
556 | * The 'vminfo' service description.
|
---|
557 | */
|
---|
558 | VBOXSERVICE g_Control =
|
---|
559 | {
|
---|
560 | /* pszName. */
|
---|
561 | "control",
|
---|
562 | /* pszDescription. */
|
---|
563 | "Host-driven Guest Control",
|
---|
564 | /* pszUsage. */
|
---|
565 | #ifdef DEBUG
|
---|
566 | " [--control-dump-stderr] [--control-dump-stdout]\n"
|
---|
567 | #endif
|
---|
568 | " [--control-interval <ms>]"
|
---|
569 | ,
|
---|
570 | /* pszOptions. */
|
---|
571 | #ifdef DEBUG
|
---|
572 | " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
|
---|
573 | " temporary directory.\n"
|
---|
574 | " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
|
---|
575 | " temporary directory.\n"
|
---|
576 | #endif
|
---|
577 | " --control-interval Specifies the interval at which to check for\n"
|
---|
578 | " new control messages. The default is 1000 ms.\n"
|
---|
579 | ,
|
---|
580 | /* methods */
|
---|
581 | vgsvcGstCtrlPreInit,
|
---|
582 | vgsvcGstCtrlOption,
|
---|
583 | vgsvcGstCtrlInit,
|
---|
584 | vgsvcGstCtrlWorker,
|
---|
585 | vgsvcGstCtrlStop,
|
---|
586 | vgsvcGstCtrlTerm
|
---|
587 | };
|
---|
588 |
|
---|