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