1 | /* $Id: VBoxServiceControl.cpp 44045 2012-12-06 08:19:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceControl - Host-driven Guest Control.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 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/file.h>
|
---|
25 | #include <iprt/getopt.h>
|
---|
26 | #include <iprt/mem.h>
|
---|
27 | #include <iprt/path.h>
|
---|
28 | #include <iprt/semaphore.h>
|
---|
29 | #include <iprt/thread.h>
|
---|
30 | #include <VBox/VBoxGuestLib.h>
|
---|
31 | #include <VBox/HostServices/GuestControlSvc.h>
|
---|
32 | #include "VBoxServiceInternal.h"
|
---|
33 | #include "VBoxServiceUtils.h"
|
---|
34 |
|
---|
35 | using namespace guestControl;
|
---|
36 |
|
---|
37 | /*******************************************************************************
|
---|
38 | * Global Variables *
|
---|
39 | *******************************************************************************/
|
---|
40 | /** The control interval (milliseconds). */
|
---|
41 | static uint32_t g_uControlIntervalMS = 0;
|
---|
42 | /** The semaphore we're blocking our main control thread on. */
|
---|
43 | static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
44 | /** The guest control service client ID. */
|
---|
45 | static uint32_t g_uControlSvcClientID = 0;
|
---|
46 | /** How many started guest processes are kept into memory for supplying
|
---|
47 | * information to the host. Default is 256 processes. If 0 is specified,
|
---|
48 | * the maximum number of processes is unlimited. */
|
---|
49 | static uint32_t g_uControlProcsMaxKept = 256;
|
---|
50 | #ifdef DEBUG
|
---|
51 | static bool g_fControlDumpStdErr = false;
|
---|
52 | static bool g_fControlDumpStdOut = false;
|
---|
53 | #endif
|
---|
54 | /** List of active guest control threads (VBOXSERVICECTRLTHREAD). */
|
---|
55 | static RTLISTANCHOR g_lstControlThreadsActive;
|
---|
56 | /** List of inactive guest control threads (VBOXSERVICECTRLTHREAD). */
|
---|
57 | static RTLISTANCHOR g_lstControlThreadsInactive;
|
---|
58 | /** Critical section protecting g_GuestControlExecThreads. */
|
---|
59 | static RTCRITSECT g_csControlThreads;
|
---|
60 | /** List of guest control files (VBOXSERVICECTRLFILE).
|
---|
61 | **@todo Use a map (later). */
|
---|
62 | static RTLISTANCHOR g_lstControlFiles;
|
---|
63 | /** The internal file count for building our internal file handles.
|
---|
64 | * Should be enough for now. */
|
---|
65 | static uint32_t g_uControlFileCount = 0;
|
---|
66 |
|
---|
67 |
|
---|
68 | /*******************************************************************************
|
---|
69 | * Internal Functions *
|
---|
70 | *******************************************************************************/
|
---|
71 | /** @todo Shorten "VBoxServiceControl" to "gstsvcCntl". */
|
---|
72 | static int VBoxServiceControlReapThreads(void);
|
---|
73 | static int VBoxServiceControlStartAllowed(bool *pbAllowed);
|
---|
74 | static int VBoxServiceControlHandleCmdStartProc(uint32_t u32ClientId, uint32_t uNumParms);
|
---|
75 | static int VBoxServiceControlHandleCmdSetInput(uint32_t u32ClientId, uint32_t uNumParms, void *pvScratchBuf, size_t cbScratchBuf);
|
---|
76 | static int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms);
|
---|
77 | static int VBoxServiceControlHandleFileOpen(uint32_t idClient, uint32_t cParms);
|
---|
78 | static int VBoxServiceControlHandleFileClose(uint32_t idClient, uint32_t cParms);
|
---|
79 | static int VBoxServiceControlHandleFileRead(uint32_t idClient, uint32_t cParms);
|
---|
80 | static int VBoxServiceControlHandleFileWrite(uint32_t idClient, uint32_t cParms, void *pvScratchBuf, size_t cbScratchBuf);
|
---|
81 | static int VBoxServiceControlHandleFileSeek(uint32_t idClient, uint32_t cParms);
|
---|
82 | static int VBoxServiceControlHandleFileTell(uint32_t idClient, uint32_t cParms);
|
---|
83 |
|
---|
84 | #ifdef DEBUG
|
---|
85 | static int vboxServiceControlDump(const char *pszFileName, void *pvBuf, size_t cbBuf)
|
---|
86 | {
|
---|
87 | AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
|
---|
88 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
89 |
|
---|
90 | if (!cbBuf)
|
---|
91 | return VINF_SUCCESS;
|
---|
92 |
|
---|
93 | char szFile[RTPATH_MAX];
|
---|
94 |
|
---|
95 | int rc = RTPathTemp(szFile, sizeof(szFile));
|
---|
96 | if (RT_SUCCESS(rc))
|
---|
97 | rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
|
---|
98 |
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
|
---|
102 |
|
---|
103 | RTFILE fh;
|
---|
104 | rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
105 | if (RT_SUCCESS(rc))
|
---|
106 | {
|
---|
107 | rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
|
---|
108 | RTFileClose(fh);
|
---|
109 | }
|
---|
110 | }
|
---|
111 |
|
---|
112 | return rc;
|
---|
113 | }
|
---|
114 | #endif
|
---|
115 |
|
---|
116 |
|
---|
117 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
118 | static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
|
---|
119 | {
|
---|
120 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
121 | /*
|
---|
122 | * Read the service options from the VM's guest properties.
|
---|
123 | * Note that these options can be overridden by the command line options later.
|
---|
124 | */
|
---|
125 | uint32_t uGuestPropSvcClientID;
|
---|
126 | int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
127 | if (RT_FAILURE(rc))
|
---|
128 | {
|
---|
129 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
130 | {
|
---|
131 | VBoxServiceVerbose(0, "Guest property service is not available, skipping\n");
|
---|
132 | rc = VINF_SUCCESS;
|
---|
133 | }
|
---|
134 | else
|
---|
135 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
136 | }
|
---|
137 | else
|
---|
138 | {
|
---|
139 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--control-procs-max-kept",
|
---|
140 | &g_uControlProcsMaxKept, 0, UINT32_MAX - 1);
|
---|
141 |
|
---|
142 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
143 | }
|
---|
144 |
|
---|
145 | if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
|
---|
146 | rc = VINF_SUCCESS;
|
---|
147 | return rc;
|
---|
148 | #else
|
---|
149 | /* Nothing to do here yet. */
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | #endif
|
---|
152 | }
|
---|
153 |
|
---|
154 |
|
---|
155 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
156 | static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
157 | {
|
---|
158 | int rc = -1;
|
---|
159 | if (ppszShort)
|
---|
160 | /* no short options */;
|
---|
161 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
162 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
163 | &g_uControlIntervalMS, 1, UINT32_MAX - 1);
|
---|
164 | else if (!strcmp(argv[*pi], "--control-procs-max-kept"))
|
---|
165 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
166 | &g_uControlProcsMaxKept, 0, UINT32_MAX - 1);
|
---|
167 | #ifdef DEBUG
|
---|
168 | else if (!strcmp(argv[*pi], "--control-dump-stderr"))
|
---|
169 | {
|
---|
170 | g_fControlDumpStdErr = true;
|
---|
171 | rc = 0; /* Flag this command as parsed. */
|
---|
172 | }
|
---|
173 | else if (!strcmp(argv[*pi], "--control-dump-stdout"))
|
---|
174 | {
|
---|
175 | g_fControlDumpStdOut = true;
|
---|
176 | rc = 0; /* Flag this command as parsed. */
|
---|
177 | }
|
---|
178 | #endif
|
---|
179 | return rc;
|
---|
180 | }
|
---|
181 |
|
---|
182 |
|
---|
183 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
184 | static DECLCALLBACK(int) VBoxServiceControlInit(void)
|
---|
185 | {
|
---|
186 | /*
|
---|
187 | * If not specified, find the right interval default.
|
---|
188 | * Then create the event sem to block on.
|
---|
189 | */
|
---|
190 | if (!g_uControlIntervalMS)
|
---|
191 | g_uControlIntervalMS = 1000;
|
---|
192 |
|
---|
193 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
194 | AssertRCReturn(rc, rc);
|
---|
195 |
|
---|
196 | rc = VbglR3GuestCtrlConnect(&g_uControlSvcClientID);
|
---|
197 | if (RT_SUCCESS(rc))
|
---|
198 | {
|
---|
199 | VBoxServiceVerbose(3, "Service client ID: %#x\n", g_uControlSvcClientID);
|
---|
200 |
|
---|
201 | /* Init lists. */
|
---|
202 | RTListInit(&g_lstControlThreadsActive);
|
---|
203 | RTListInit(&g_lstControlThreadsInactive);
|
---|
204 | RTListInit(&g_lstControlFiles);
|
---|
205 |
|
---|
206 | /* Init critical section for protecting the thread lists. */
|
---|
207 | rc = RTCritSectInit(&g_csControlThreads);
|
---|
208 | AssertRC(rc);
|
---|
209 | }
|
---|
210 | else
|
---|
211 | {
|
---|
212 | /* If the service was not found, we disable this service without
|
---|
213 | causing VBoxService to fail. */
|
---|
214 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
215 | {
|
---|
216 | VBoxServiceVerbose(0, "Guest control service is not available\n");
|
---|
217 | rc = VERR_SERVICE_DISABLED;
|
---|
218 | }
|
---|
219 | else
|
---|
220 | VBoxServiceError("Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
221 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
222 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
223 | }
|
---|
224 | return rc;
|
---|
225 | }
|
---|
226 |
|
---|
227 |
|
---|
228 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
229 | DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
|
---|
230 | {
|
---|
231 | /*
|
---|
232 | * Tell the control thread that it can continue
|
---|
233 | * spawning services.
|
---|
234 | */
|
---|
235 | RTThreadUserSignal(RTThreadSelf());
|
---|
236 | Assert(g_uControlSvcClientID > 0);
|
---|
237 |
|
---|
238 | int rc = VINF_SUCCESS;
|
---|
239 |
|
---|
240 | /* Allocate a scratch buffer for commands which also send
|
---|
241 | * payload data with them. */
|
---|
242 | uint32_t cbScratchBuf = _64K; /** @todo Make buffer size configurable via guest properties/argv! */
|
---|
243 | AssertReturn(RT_IS_POWER_OF_TWO(cbScratchBuf), VERR_INVALID_PARAMETER);
|
---|
244 | uint8_t *pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
|
---|
245 | AssertPtrReturn(pvScratchBuf, VERR_NO_MEMORY);
|
---|
246 |
|
---|
247 | /*
|
---|
248 | * Execution loop.
|
---|
249 | *
|
---|
250 | * @todo
|
---|
251 | */
|
---|
252 | for (;;)
|
---|
253 | {
|
---|
254 | VBoxServiceVerbose(3, "Waiting for host msg ...\n");
|
---|
255 | uint32_t uMsg = 0;
|
---|
256 | uint32_t cParms = 0;
|
---|
257 | rc = VbglR3GuestCtrlWaitForHostMsg(g_uControlSvcClientID, &uMsg, &cParms);
|
---|
258 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
259 | {
|
---|
260 | VBoxServiceVerbose(4, "Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
|
---|
261 | rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
|
---|
262 | }
|
---|
263 | else if (RT_FAILURE(rc))
|
---|
264 | VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
|
---|
265 | if (RT_SUCCESS(rc))
|
---|
266 | {
|
---|
267 | VBoxServiceVerbose(3, "Msg=%u (%u parms) retrieved\n", uMsg, cParms);
|
---|
268 | switch (uMsg)
|
---|
269 | {
|
---|
270 | case HOST_CANCEL_PENDING_WAITS:
|
---|
271 | VBoxServiceVerbose(3, "Host asked us to quit ...\n");
|
---|
272 | break;
|
---|
273 |
|
---|
274 | case HOST_EXEC_CMD:
|
---|
275 | rc = VBoxServiceControlHandleCmdStartProc(g_uControlSvcClientID, cParms);
|
---|
276 | break;
|
---|
277 |
|
---|
278 | case HOST_EXEC_SET_INPUT:
|
---|
279 | rc = VBoxServiceControlHandleCmdSetInput(g_uControlSvcClientID, cParms,
|
---|
280 | pvScratchBuf, cbScratchBuf);
|
---|
281 | break;
|
---|
282 |
|
---|
283 | case HOST_EXEC_GET_OUTPUT:
|
---|
284 | rc = VBoxServiceControlHandleCmdGetOutput(g_uControlSvcClientID, cParms);
|
---|
285 | break;
|
---|
286 |
|
---|
287 | case HOST_FILE_OPEN:
|
---|
288 | rc = VBoxServiceControlHandleFileOpen(g_uControlSvcClientID, cParms);
|
---|
289 | break;
|
---|
290 |
|
---|
291 | case HOST_FILE_CLOSE:
|
---|
292 | rc = VBoxServiceControlHandleFileClose(g_uControlSvcClientID, cParms);
|
---|
293 | break;
|
---|
294 |
|
---|
295 | case HOST_FILE_READ:
|
---|
296 | rc = VBoxServiceControlHandleFileRead(g_uControlSvcClientID, cParms);
|
---|
297 | break;
|
---|
298 |
|
---|
299 | case HOST_FILE_WRITE:
|
---|
300 | rc = VBoxServiceControlHandleFileWrite(g_uControlSvcClientID, cParms,
|
---|
301 | pvScratchBuf, cbScratchBuf);
|
---|
302 | break;
|
---|
303 |
|
---|
304 | case HOST_FILE_SEEK:
|
---|
305 | rc = VBoxServiceControlHandleFileSeek(g_uControlSvcClientID, cParms);
|
---|
306 | break;
|
---|
307 |
|
---|
308 | case HOST_FILE_TELL:
|
---|
309 | rc = VBoxServiceControlHandleFileTell(g_uControlSvcClientID, cParms);
|
---|
310 | break;
|
---|
311 |
|
---|
312 | default:
|
---|
313 | VBoxServiceVerbose(3, "Unsupported message from host! Msg=%u\n", uMsg);
|
---|
314 | /* Don't terminate here; just wait for the next message. */
|
---|
315 | break;
|
---|
316 | }
|
---|
317 | }
|
---|
318 |
|
---|
319 | /* Do we need to shutdown? */
|
---|
320 | if ( *pfShutdown
|
---|
321 | || (RT_SUCCESS(rc) && uMsg == HOST_CANCEL_PENDING_WAITS))
|
---|
322 | {
|
---|
323 | rc = VINF_SUCCESS;
|
---|
324 | break;
|
---|
325 | }
|
---|
326 |
|
---|
327 | /* Let's sleep for a bit and let others run ... */
|
---|
328 | RTThreadYield();
|
---|
329 | }
|
---|
330 |
|
---|
331 | /* Delete scratch buffer. */
|
---|
332 | if (pvScratchBuf)
|
---|
333 | RTMemFree(pvScratchBuf);
|
---|
334 |
|
---|
335 | return rc;
|
---|
336 | }
|
---|
337 |
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Handles starting processes on the guest.
|
---|
341 | *
|
---|
342 | * @returns IPRT status code.
|
---|
343 | * @param uClientID The HGCM client session ID.
|
---|
344 | * @param cParms The number of parameters the host is offering.
|
---|
345 | */
|
---|
346 | static int VBoxServiceControlHandleCmdStartProc(uint32_t uClientID, uint32_t cParms)
|
---|
347 | {
|
---|
348 | uint32_t uContextID = 0;
|
---|
349 |
|
---|
350 | int rc;
|
---|
351 | bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
|
---|
352 | if (cParms == 11)
|
---|
353 | {
|
---|
354 | VBOXSERVICECTRLPROCESS proc;
|
---|
355 | RT_ZERO(proc);
|
---|
356 |
|
---|
357 | /* Initialize maximum environment block size -- needed as input
|
---|
358 | * parameter to retrieve the stuff from the host. On output this then
|
---|
359 | * will contain the actual block size. */
|
---|
360 | proc.cbEnv = sizeof(proc.szEnv);
|
---|
361 |
|
---|
362 | rc = VbglR3GuestCtrlExecGetHostCmdExec(uClientID,
|
---|
363 | cParms,
|
---|
364 | &uContextID,
|
---|
365 | /* Command */
|
---|
366 | proc.szCmd, sizeof(proc.szCmd),
|
---|
367 | /* Flags */
|
---|
368 | &proc.uFlags,
|
---|
369 | /* Arguments */
|
---|
370 | proc.szArgs, sizeof(proc.szArgs), &proc.uNumArgs,
|
---|
371 | /* Environment */
|
---|
372 | proc.szEnv, &proc.cbEnv, &proc.uNumEnvVars,
|
---|
373 | /* Credentials */
|
---|
374 | proc.szUser, sizeof(proc.szUser),
|
---|
375 | proc.szPassword, sizeof(proc.szPassword),
|
---|
376 | /* Timelimit */
|
---|
377 | &proc.uTimeLimitMS);
|
---|
378 | if (RT_SUCCESS(rc))
|
---|
379 | {
|
---|
380 | VBoxServiceVerbose(3, "Request to start process szCmd=%s, uFlags=0x%x, szArgs=%s, szEnv=%s, szUser=%s, szPassword=%s, uTimeout=%u\n",
|
---|
381 | proc.szCmd, proc.uFlags,
|
---|
382 | proc.uNumArgs ? proc.szArgs : "<None>",
|
---|
383 | proc.uNumEnvVars ? proc.szEnv : "<None>",
|
---|
384 | proc.szUser,
|
---|
385 | #ifdef DEBUG
|
---|
386 | proc.szPassword,
|
---|
387 | #else
|
---|
388 | "XXX", /* Never show passwords in release mode. */
|
---|
389 | #endif
|
---|
390 | proc.uTimeLimitMS);
|
---|
391 |
|
---|
392 | rc = VBoxServiceControlReapThreads();
|
---|
393 | if (RT_FAILURE(rc))
|
---|
394 | VBoxServiceError("Reaping stopped processes failed with rc=%Rrc\n", rc);
|
---|
395 | /* Keep going. */
|
---|
396 |
|
---|
397 | rc = VBoxServiceControlStartAllowed(&fStartAllowed);
|
---|
398 | if (RT_SUCCESS(rc))
|
---|
399 | {
|
---|
400 | if (fStartAllowed)
|
---|
401 | {
|
---|
402 | rc = VBoxServiceControlThreadStart(uContextID, &proc);
|
---|
403 | }
|
---|
404 | else
|
---|
405 | rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
|
---|
406 | }
|
---|
407 | }
|
---|
408 | }
|
---|
409 | else
|
---|
410 | rc = VERR_INVALID_PARAMETER; /* Incorrect number of parameters. */
|
---|
411 |
|
---|
412 | /* In case of an error we need to notify the host to not wait forever for our response. */
|
---|
413 | if (RT_FAILURE(rc))
|
---|
414 | {
|
---|
415 | VBoxServiceError("Starting process failed with rc=%Rrc\n", rc);
|
---|
416 |
|
---|
417 | /*
|
---|
418 | * Note: The context ID can be 0 because we mabye weren't able to fetch the command
|
---|
419 | * from the host. The host in case has to deal with that!
|
---|
420 | */
|
---|
421 | int rc2 = VbglR3GuestCtrlExecReportStatus(uClientID, uContextID /* Might be 0 */, 0 /* PID, invalid */,
|
---|
422 | PROC_STS_ERROR, rc,
|
---|
423 | NULL /* pvData */, 0 /* cbData */);
|
---|
424 | if (RT_FAILURE(rc2))
|
---|
425 | {
|
---|
426 | VBoxServiceError("Error sending start process status to host, rc=%Rrc\n", rc2);
|
---|
427 | if (RT_SUCCESS(rc))
|
---|
428 | rc = rc2;
|
---|
429 | }
|
---|
430 | }
|
---|
431 |
|
---|
432 | return rc;
|
---|
433 | }
|
---|
434 |
|
---|
435 |
|
---|
436 | /**
|
---|
437 | * Gets output from stdout/stderr of a specified guest process.
|
---|
438 | *
|
---|
439 | * @return IPRT status code.
|
---|
440 | * @param uPID PID of process to retrieve the output from.
|
---|
441 | * @param uHandleId Stream ID (stdout = 0, stderr = 2) to get the output from.
|
---|
442 | * @param cMsTimeout Timeout (in ms) to wait for output becoming
|
---|
443 | * available.
|
---|
444 | * @param pvBuf Pointer to a pre-allocated buffer to store the output.
|
---|
445 | * @param cbBuf Size (in bytes) of the pre-allocated buffer.
|
---|
446 | * @param pcbRead Pointer to number of bytes read. Optional.
|
---|
447 | */
|
---|
448 | int VBoxServiceControlExecGetOutput(uint32_t uPID, uint32_t uCID,
|
---|
449 | uint32_t uHandleId, uint32_t cMsTimeout,
|
---|
450 | void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
451 | {
|
---|
452 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
453 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
454 | AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
|
---|
455 |
|
---|
456 | int rc = VINF_SUCCESS;
|
---|
457 | VBOXSERVICECTRLREQUESTTYPE reqType = VBOXSERVICECTRLREQUEST_UNKNOWN; /* (gcc maybe, well, wrong.) */
|
---|
458 | switch (uHandleId)
|
---|
459 | {
|
---|
460 | case OUTPUT_HANDLE_ID_STDERR:
|
---|
461 | reqType = VBOXSERVICECTRLREQUEST_STDERR_READ;
|
---|
462 | break;
|
---|
463 |
|
---|
464 | case OUTPUT_HANDLE_ID_STDOUT:
|
---|
465 | case OUTPUT_HANDLE_ID_STDOUT_DEPRECATED:
|
---|
466 | reqType = VBOXSERVICECTRLREQUEST_STDOUT_READ;
|
---|
467 | break;
|
---|
468 |
|
---|
469 | default:
|
---|
470 | rc = VERR_INVALID_PARAMETER;
|
---|
471 | break;
|
---|
472 | }
|
---|
473 |
|
---|
474 | if (RT_SUCCESS(rc))
|
---|
475 | {
|
---|
476 | PVBOXSERVICECTRLREQUEST pRequest;
|
---|
477 | rc = VBoxServiceControlThreadRequestAllocEx(&pRequest, reqType, pvBuf, cbBuf, uCID);
|
---|
478 | if (RT_SUCCESS(rc))
|
---|
479 | {
|
---|
480 | rc = VBoxServiceControlThreadPerform(uPID, pRequest);
|
---|
481 | if (RT_SUCCESS(rc) && pcbRead)
|
---|
482 | *pcbRead = pRequest->cbData;
|
---|
483 | VBoxServiceControlThreadRequestFree(pRequest);
|
---|
484 | }
|
---|
485 | }
|
---|
486 |
|
---|
487 | return rc;
|
---|
488 | }
|
---|
489 |
|
---|
490 |
|
---|
491 | /**
|
---|
492 | * Sets the specified guest thread to a certain list.
|
---|
493 | *
|
---|
494 | * @return IPRT status code.
|
---|
495 | * @param enmList List to move thread to.
|
---|
496 | * @param pThread Thread to set inactive.
|
---|
497 | */
|
---|
498 | int VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLISTTYPE enmList,
|
---|
499 | PVBOXSERVICECTRLTHREAD pThread)
|
---|
500 | {
|
---|
501 | AssertReturn(enmList > VBOXSERVICECTRLTHREADLIST_UNKNOWN, VERR_INVALID_PARAMETER);
|
---|
502 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
503 |
|
---|
504 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
505 | if (RT_SUCCESS(rc))
|
---|
506 | {
|
---|
507 | VBoxServiceVerbose(3, "Setting thread (PID %u) to list %d\n",
|
---|
508 | pThread->uPID, enmList);
|
---|
509 |
|
---|
510 | PRTLISTANCHOR pAnchor = NULL;
|
---|
511 | switch (enmList)
|
---|
512 | {
|
---|
513 | case VBOXSERVICECTRLTHREADLIST_STOPPED:
|
---|
514 | pAnchor = &g_lstControlThreadsInactive;
|
---|
515 | break;
|
---|
516 |
|
---|
517 | case VBOXSERVICECTRLTHREADLIST_RUNNING:
|
---|
518 | pAnchor = &g_lstControlThreadsActive;
|
---|
519 | break;
|
---|
520 |
|
---|
521 | default:
|
---|
522 | AssertMsgFailed(("Unknown list type: %u", enmList));
|
---|
523 | break;
|
---|
524 | }
|
---|
525 |
|
---|
526 | if (!pAnchor)
|
---|
527 | rc = VERR_INVALID_PARAMETER;
|
---|
528 |
|
---|
529 | if (RT_SUCCESS(rc))
|
---|
530 | {
|
---|
531 | if (pThread->pAnchor != NULL)
|
---|
532 | {
|
---|
533 | /* If thread was assigned to a list before,
|
---|
534 | * remove the thread from the old list first. */
|
---|
535 | /* rc = */ RTListNodeRemove(&pThread->Node);
|
---|
536 | }
|
---|
537 |
|
---|
538 | /* Add thread to desired list. */
|
---|
539 | /* rc = */ RTListAppend(pAnchor, &pThread->Node);
|
---|
540 | pThread->pAnchor = pAnchor;
|
---|
541 | }
|
---|
542 |
|
---|
543 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
544 | if (RT_SUCCESS(rc))
|
---|
545 | rc = rc2;
|
---|
546 | }
|
---|
547 |
|
---|
548 | return VINF_SUCCESS;
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * Injects input to a specified running process.
|
---|
554 | *
|
---|
555 | * @return IPRT status code.
|
---|
556 | * @param uPID PID of process to set the input for.
|
---|
557 | * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
|
---|
558 | * @param pvBuf Pointer to a buffer containing the actual input data.
|
---|
559 | * @param cbBuf Size (in bytes) of the input buffer data.
|
---|
560 | * @param pcbWritten Pointer to number of bytes written to the process. Optional.
|
---|
561 | */
|
---|
562 | int VBoxServiceControlSetInput(uint32_t uPID, uint32_t uCID,
|
---|
563 | bool fPendingClose,
|
---|
564 | void *pvBuf, uint32_t cbBuf,
|
---|
565 | uint32_t *pcbWritten)
|
---|
566 | {
|
---|
567 | /* pvBuf is optional. */
|
---|
568 | /* cbBuf is optional. */
|
---|
569 | /* pcbWritten is optional. */
|
---|
570 |
|
---|
571 | PVBOXSERVICECTRLREQUEST pRequest;
|
---|
572 | int rc = VBoxServiceControlThreadRequestAllocEx(&pRequest,
|
---|
573 | fPendingClose
|
---|
574 | ? VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF
|
---|
575 | : VBOXSERVICECTRLREQUEST_STDIN_WRITE,
|
---|
576 | pvBuf, cbBuf, uCID);
|
---|
577 | if (RT_SUCCESS(rc))
|
---|
578 | {
|
---|
579 | rc = VBoxServiceControlThreadPerform(uPID, pRequest);
|
---|
580 | if (RT_SUCCESS(rc))
|
---|
581 | {
|
---|
582 | if (pcbWritten)
|
---|
583 | *pcbWritten = pRequest->cbData;
|
---|
584 | }
|
---|
585 |
|
---|
586 | VBoxServiceControlThreadRequestFree(pRequest);
|
---|
587 | }
|
---|
588 |
|
---|
589 | return rc;
|
---|
590 | }
|
---|
591 |
|
---|
592 |
|
---|
593 | /**
|
---|
594 | * Handles input for a started process by copying the received data into its
|
---|
595 | * stdin pipe.
|
---|
596 | *
|
---|
597 | * @returns IPRT status code.
|
---|
598 | * @param idClient The HGCM client session ID.
|
---|
599 | * @param cParms The number of parameters the host is
|
---|
600 | * offering.
|
---|
601 | * @param pvScratchBuf The scratch buffer.
|
---|
602 | * @param cbScratchBuf The scratch buffer size for retrieving the input data.
|
---|
603 | */
|
---|
604 | static int VBoxServiceControlHandleCmdSetInput(uint32_t idClient, uint32_t cParms,
|
---|
605 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
606 | {
|
---|
607 | AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
|
---|
608 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
609 |
|
---|
610 | uint32_t uContextID;
|
---|
611 | uint32_t uPID;
|
---|
612 | uint32_t uFlags;
|
---|
613 | uint32_t cbSize;
|
---|
614 |
|
---|
615 | uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
|
---|
616 | uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
|
---|
617 |
|
---|
618 | /*
|
---|
619 | * Ask the host for the input data.
|
---|
620 | */
|
---|
621 | int rc = VbglR3GuestCtrlExecGetHostCmdInput(idClient, cParms,
|
---|
622 | &uContextID, &uPID, &uFlags,
|
---|
623 | pvScratchBuf, cbScratchBuf, &cbSize);
|
---|
624 | if (RT_FAILURE(rc))
|
---|
625 | {
|
---|
626 | VBoxServiceError("[PID %u]: Failed to retrieve exec input command! Error: %Rrc\n",
|
---|
627 | uPID, rc);
|
---|
628 | }
|
---|
629 | else if (cbSize > cbScratchBuf)
|
---|
630 | {
|
---|
631 | VBoxServiceError("[PID %u]: Too much input received! cbSize=%u, cbScratchBuf=%u\n",
|
---|
632 | uPID, cbSize, cbScratchBuf);
|
---|
633 | rc = VERR_INVALID_PARAMETER;
|
---|
634 | }
|
---|
635 | else
|
---|
636 | {
|
---|
637 | /*
|
---|
638 | * Is this the last input block we need to deliver? Then let the pipe know ...
|
---|
639 | */
|
---|
640 | bool fPendingClose = false;
|
---|
641 | if (uFlags & INPUT_FLAG_EOF)
|
---|
642 | {
|
---|
643 | fPendingClose = true;
|
---|
644 | VBoxServiceVerbose(4, "[PID %u]: Got last input block of size %u ...\n",
|
---|
645 | uPID, cbSize);
|
---|
646 | }
|
---|
647 |
|
---|
648 | rc = VBoxServiceControlSetInput(uPID, uContextID, fPendingClose, pvScratchBuf,
|
---|
649 | cbSize, &cbWritten);
|
---|
650 | VBoxServiceVerbose(4, "[PID %u]: Written input, CID=%u, rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
|
---|
651 | uPID, uContextID, rc, uFlags, fPendingClose, cbSize, cbWritten);
|
---|
652 | if (RT_SUCCESS(rc))
|
---|
653 | {
|
---|
654 | uStatus = INPUT_STS_WRITTEN;
|
---|
655 | uFlags = 0; /* No flags at the moment. */
|
---|
656 | }
|
---|
657 | else
|
---|
658 | {
|
---|
659 | if (rc == VERR_BAD_PIPE)
|
---|
660 | uStatus = INPUT_STS_TERMINATED;
|
---|
661 | else if (rc == VERR_BUFFER_OVERFLOW)
|
---|
662 | uStatus = INPUT_STS_OVERFLOW;
|
---|
663 | }
|
---|
664 | }
|
---|
665 |
|
---|
666 | /*
|
---|
667 | * If there was an error and we did not set the host status
|
---|
668 | * yet, then do it now.
|
---|
669 | */
|
---|
670 | if ( RT_FAILURE(rc)
|
---|
671 | && uStatus == INPUT_STS_UNDEFINED)
|
---|
672 | {
|
---|
673 | uStatus = INPUT_STS_ERROR;
|
---|
674 | uFlags = rc;
|
---|
675 | }
|
---|
676 | Assert(uStatus > INPUT_STS_UNDEFINED);
|
---|
677 |
|
---|
678 | VBoxServiceVerbose(3, "[PID %u]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
|
---|
679 | uPID, uContextID, uStatus, uFlags, cbWritten);
|
---|
680 |
|
---|
681 | /* Note: Since the context ID is unique the request *has* to be completed here,
|
---|
682 | * regardless whether we got data or not! Otherwise the progress object
|
---|
683 | * on the host never will get completed! */
|
---|
684 | rc = VbglR3GuestCtrlExecReportStatusIn(idClient, uContextID, uPID,
|
---|
685 | uStatus, uFlags, (uint32_t)cbWritten);
|
---|
686 |
|
---|
687 | if (RT_FAILURE(rc))
|
---|
688 | VBoxServiceError("[PID %u]: Failed to report input status! Error: %Rrc\n",
|
---|
689 | uPID, rc);
|
---|
690 | return rc;
|
---|
691 | }
|
---|
692 |
|
---|
693 |
|
---|
694 | static PVBOXSERVICECTRLFILE VBoxControlGetFile(uint32_t uHandle)
|
---|
695 | {
|
---|
696 | PVBOXSERVICECTRLFILE pFileCur = NULL;
|
---|
697 | /** @todo Use a map later! */
|
---|
698 | RTListForEach(&g_lstControlFiles, pFileCur, VBOXSERVICECTRLFILE, Node)
|
---|
699 | {
|
---|
700 | if (pFileCur->uHandle == uHandle)
|
---|
701 | return pFileCur;
|
---|
702 | }
|
---|
703 |
|
---|
704 | return NULL;
|
---|
705 | }
|
---|
706 |
|
---|
707 |
|
---|
708 | static int VBoxServiceControlHandleFileOpen(uint32_t idClient, uint32_t cParms)
|
---|
709 | {
|
---|
710 | uint32_t uContextID;
|
---|
711 |
|
---|
712 | char szFile[RTPATH_MAX];
|
---|
713 | char szOpenMode[64];
|
---|
714 | char szDisposition[64];
|
---|
715 | uint32_t uCreationMode;
|
---|
716 | uint64_t uOffset;
|
---|
717 |
|
---|
718 | int rc = VbglR3GuestCtrlFileGetHostCmdOpen(idClient, cParms, &uContextID,
|
---|
719 | /* File to open. */
|
---|
720 | szFile, sizeof(szFile),
|
---|
721 | /* Open mode. */
|
---|
722 | szOpenMode, sizeof(szOpenMode),
|
---|
723 | /* Disposition. */
|
---|
724 | szDisposition, sizeof(szDisposition),
|
---|
725 | /* Creation mode. */
|
---|
726 | &uCreationMode,
|
---|
727 | /* Offset. */
|
---|
728 | &uOffset);
|
---|
729 | if (RT_SUCCESS(rc))
|
---|
730 | {
|
---|
731 | PVBOXSERVICECTRLFILE pFile = (PVBOXSERVICECTRLFILE)RTMemAlloc(sizeof(VBOXSERVICECTRLFILE));
|
---|
732 | if (!pFile)
|
---|
733 | return VERR_NO_MEMORY;
|
---|
734 |
|
---|
735 | if (!RTStrPrintf(pFile->szName, sizeof(pFile->szName), "%s", szFile))
|
---|
736 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
737 |
|
---|
738 | if (RT_SUCCESS(rc))
|
---|
739 | {
|
---|
740 | uint64_t fFlags = RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE; /** @todo Modes! */
|
---|
741 | rc = RTFileOpen(&pFile->hFile, pFile->szName, fFlags);
|
---|
742 | if ( RT_SUCCESS(rc)
|
---|
743 | && uOffset)
|
---|
744 | {
|
---|
745 | /* Seeking is optional. */
|
---|
746 | int rc2 = RTFileSeek(pFile->hFile, (int64_t)uOffset, RTFILE_SEEK_BEGIN, NULL /* Current offset */);
|
---|
747 | if (RT_FAILURE(rc2))
|
---|
748 | VBoxServiceVerbose(3, "[File %s]: Seeking to offset %RU64 failed; rc=%Rrc\n",
|
---|
749 | pFile->szName, uOffset, rc);
|
---|
750 | }
|
---|
751 | else
|
---|
752 | VBoxServiceVerbose(3, "[File %s]: Opening failed; rc=%Rrc\n",
|
---|
753 | pFile->szName, rc);
|
---|
754 | }
|
---|
755 |
|
---|
756 | uint32_t uHandle = 0;
|
---|
757 | if (RT_SUCCESS(rc))
|
---|
758 | {
|
---|
759 | VBoxServiceVerbose(3, "[File %s]: Opened.\n", pFile->szName);
|
---|
760 |
|
---|
761 | uHandle = g_uControlFileCount++;
|
---|
762 | pFile->uHandle = uHandle;
|
---|
763 | /* rc = */ RTListAppend(&g_lstControlFiles, &pFile->Node);
|
---|
764 | }
|
---|
765 |
|
---|
766 | if (RT_FAILURE(rc))
|
---|
767 | RTMemFree(pFile);
|
---|
768 |
|
---|
769 | /* Report back in any case. */
|
---|
770 | int rc2 = VbglR3GuestCtrlFileNotify(idClient, uContextID, uHandle,
|
---|
771 | GUESTFILENOTIFYTYPE_OPEN, &rc, sizeof(rc));
|
---|
772 | if (RT_FAILURE(rc2))
|
---|
773 | VBoxServiceError("[File %s]: Failed to report open status, rc=%Rrc\n",
|
---|
774 | szFile, rc2);
|
---|
775 | if (RT_SUCCESS(rc))
|
---|
776 | rc = rc2;
|
---|
777 | }
|
---|
778 | return rc;
|
---|
779 | }
|
---|
780 |
|
---|
781 |
|
---|
782 | static int VBoxServiceControlHandleFileClose(uint32_t idClient, uint32_t cParms)
|
---|
783 | {
|
---|
784 | uint32_t uContextID;
|
---|
785 | uint32_t uHandle;
|
---|
786 |
|
---|
787 | int rc = VbglR3GuestCtrlFileGetHostCmdClose(idClient, cParms, &uContextID,
|
---|
788 | /* File handle to close. */
|
---|
789 | &uHandle);
|
---|
790 | if (RT_SUCCESS(rc))
|
---|
791 | {
|
---|
792 | PVBOXSERVICECTRLFILE pFile = VBoxControlGetFile(uHandle);
|
---|
793 | if (pFile)
|
---|
794 | {
|
---|
795 | rc = RTFileClose(pFile->hFile);
|
---|
796 | }
|
---|
797 | else
|
---|
798 | rc = VERR_NOT_FOUND;
|
---|
799 |
|
---|
800 | /* Report back in any case. */
|
---|
801 | int rc2 = VbglR3GuestCtrlFileNotify(idClient, uContextID, uHandle,
|
---|
802 | GUESTFILENOTIFYTYPE_CLOSE, &rc, sizeof(rc));
|
---|
803 | if (RT_FAILURE(rc2))
|
---|
804 | VBoxServiceError("Failed to report close status, rc=%Rrc\n", rc2);
|
---|
805 | if (RT_SUCCESS(rc))
|
---|
806 | rc = rc2;
|
---|
807 | }
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | static int VBoxServiceControlHandleFileRead(uint32_t idClient, uint32_t cParms)
|
---|
813 | {
|
---|
814 | uint32_t uContextID;
|
---|
815 | uint32_t uHandle;
|
---|
816 | uint32_t cbToRead;
|
---|
817 |
|
---|
818 | int rc = VbglR3GuestCtrlFileGetHostCmdRead(idClient, cParms, &uContextID,
|
---|
819 | &uHandle, &cbToRead);
|
---|
820 | if (RT_SUCCESS(rc))
|
---|
821 | {
|
---|
822 |
|
---|
823 | }
|
---|
824 | return rc;
|
---|
825 | }
|
---|
826 |
|
---|
827 |
|
---|
828 | static int VBoxServiceControlHandleFileWrite(uint32_t idClient, uint32_t cParms,
|
---|
829 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
830 | {
|
---|
831 | AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
|
---|
832 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
833 |
|
---|
834 | uint32_t uContextID;
|
---|
835 | uint32_t uHandle;
|
---|
836 | uint32_t cbToWrite;
|
---|
837 |
|
---|
838 | int rc = VbglR3GuestCtrlFileGetHostCmdWrite(idClient, cParms, &uContextID,
|
---|
839 | &uHandle, pvScratchBuf, cbScratchBuf,
|
---|
840 | &cbToWrite);
|
---|
841 | if (RT_SUCCESS(rc))
|
---|
842 | {
|
---|
843 |
|
---|
844 | }
|
---|
845 | return rc;
|
---|
846 | }
|
---|
847 |
|
---|
848 |
|
---|
849 | static int VBoxServiceControlHandleFileSeek(uint32_t idClient, uint32_t cParms)
|
---|
850 | {
|
---|
851 | uint32_t uContextID;
|
---|
852 | uint32_t uHandle;
|
---|
853 | uint32_t uSeekMethod;
|
---|
854 | uint64_t uOffset; /* Will be converted to int64_t. */
|
---|
855 |
|
---|
856 | int rc = VbglR3GuestCtrlFileGetHostCmdSeek(idClient, cParms, &uContextID,
|
---|
857 | &uHandle, &uSeekMethod, &uOffset);
|
---|
858 | if (RT_SUCCESS(rc))
|
---|
859 | {
|
---|
860 |
|
---|
861 | }
|
---|
862 | return rc;
|
---|
863 | }
|
---|
864 |
|
---|
865 |
|
---|
866 | static int VBoxServiceControlHandleFileTell(uint32_t idClient, uint32_t cParms)
|
---|
867 | {
|
---|
868 | uint32_t uContextID;
|
---|
869 | uint32_t uHandle;
|
---|
870 |
|
---|
871 | int rc = VbglR3GuestCtrlFileGetHostCmdTell(idClient, cParms, &uContextID,
|
---|
872 | &uHandle);
|
---|
873 | if (RT_SUCCESS(rc))
|
---|
874 | {
|
---|
875 |
|
---|
876 | }
|
---|
877 | return rc;
|
---|
878 | }
|
---|
879 |
|
---|
880 |
|
---|
881 | /**
|
---|
882 | * Handles the guest control output command.
|
---|
883 | *
|
---|
884 | * @return IPRT status code.
|
---|
885 | * @param idClient The HGCM client session ID.
|
---|
886 | * @param cParms The number of parameters the host is offering.
|
---|
887 | */
|
---|
888 | static int VBoxServiceControlHandleCmdGetOutput(uint32_t idClient, uint32_t cParms)
|
---|
889 | {
|
---|
890 | uint32_t uContextID;
|
---|
891 | uint32_t uPID;
|
---|
892 | uint32_t uHandleID;
|
---|
893 | uint32_t uFlags;
|
---|
894 |
|
---|
895 | int rc = VbglR3GuestCtrlExecGetHostCmdOutput(idClient, cParms,
|
---|
896 | &uContextID, &uPID, &uHandleID, &uFlags);
|
---|
897 | if (RT_SUCCESS(rc))
|
---|
898 | {
|
---|
899 | uint8_t *pBuf = (uint8_t*)RTMemAlloc(_64K);
|
---|
900 | if (pBuf)
|
---|
901 | {
|
---|
902 | uint32_t cbRead = 0;
|
---|
903 | rc = VBoxServiceControlExecGetOutput(uPID, uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
|
---|
904 | pBuf, _64K /* cbSize */, &cbRead);
|
---|
905 | VBoxServiceVerbose(3, "[PID %u]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
|
---|
906 | uPID, rc, uContextID, cbRead, uHandleID, uFlags);
|
---|
907 |
|
---|
908 | #ifdef DEBUG
|
---|
909 | if ( g_fControlDumpStdErr
|
---|
910 | && uHandleID == OUTPUT_HANDLE_ID_STDERR)
|
---|
911 | {
|
---|
912 | char szPID[RTPATH_MAX];
|
---|
913 | if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
|
---|
914 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
915 | if (RT_SUCCESS(rc))
|
---|
916 | rc = vboxServiceControlDump(szPID, pBuf, cbRead);
|
---|
917 | }
|
---|
918 | else if ( g_fControlDumpStdOut
|
---|
919 | && ( uHandleID == OUTPUT_HANDLE_ID_STDOUT
|
---|
920 | || uHandleID == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED))
|
---|
921 | {
|
---|
922 | char szPID[RTPATH_MAX];
|
---|
923 | if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
|
---|
924 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
925 | if (RT_SUCCESS(rc))
|
---|
926 | rc = vboxServiceControlDump(szPID, pBuf, cbRead);
|
---|
927 | AssertRC(rc);
|
---|
928 | }
|
---|
929 | #endif
|
---|
930 | /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
|
---|
931 | * data which the host needs to work with -- so just pass through all data unfiltered! */
|
---|
932 |
|
---|
933 | /* Note: Since the context ID is unique the request *has* to be completed here,
|
---|
934 | * regardless whether we got data or not! Otherwise the progress object
|
---|
935 | * on the host never will get completed! */
|
---|
936 | int rc2 = VbglR3GuestCtrlExecSendOut(idClient, uContextID, uPID, uHandleID, uFlags,
|
---|
937 | pBuf, cbRead);
|
---|
938 | if (RT_SUCCESS(rc))
|
---|
939 | rc = rc2;
|
---|
940 | else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
|
---|
941 | rc = VINF_SUCCESS;
|
---|
942 |
|
---|
943 | RTMemFree(pBuf);
|
---|
944 | }
|
---|
945 | else
|
---|
946 | rc = VERR_NO_MEMORY;
|
---|
947 | }
|
---|
948 |
|
---|
949 | if (RT_FAILURE(rc))
|
---|
950 | VBoxServiceError("[PID %u]: Error handling output command! Error: %Rrc\n",
|
---|
951 | uPID, rc);
|
---|
952 | return rc;
|
---|
953 | }
|
---|
954 |
|
---|
955 |
|
---|
956 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
957 | static DECLCALLBACK(void) VBoxServiceControlStop(void)
|
---|
958 | {
|
---|
959 | VBoxServiceVerbose(3, "Stopping ...\n");
|
---|
960 |
|
---|
961 | /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
|
---|
962 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
963 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
964 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
965 |
|
---|
966 | /*
|
---|
967 | * Ask the host service to cancel all pending requests so that we can
|
---|
968 | * shutdown properly here.
|
---|
969 | */
|
---|
970 | if (g_uControlSvcClientID)
|
---|
971 | {
|
---|
972 | VBoxServiceVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
|
---|
973 | g_uControlSvcClientID);
|
---|
974 |
|
---|
975 | int rc = VbglR3GuestCtrlCancelPendingWaits(g_uControlSvcClientID);
|
---|
976 | if (RT_FAILURE(rc))
|
---|
977 | VBoxServiceError("Cancelling pending waits failed; rc=%Rrc\n", rc);
|
---|
978 | }
|
---|
979 | }
|
---|
980 |
|
---|
981 |
|
---|
982 | /**
|
---|
983 | * Reaps all inactive guest process threads.
|
---|
984 | *
|
---|
985 | * @return IPRT status code.
|
---|
986 | */
|
---|
987 | static int VBoxServiceControlReapThreads(void)
|
---|
988 | {
|
---|
989 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
990 | if (RT_SUCCESS(rc))
|
---|
991 | {
|
---|
992 | PVBOXSERVICECTRLTHREAD pThread =
|
---|
993 | RTListGetFirst(&g_lstControlThreadsInactive, VBOXSERVICECTRLTHREAD, Node);
|
---|
994 | while (pThread)
|
---|
995 | {
|
---|
996 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
997 | bool fLast = RTListNodeIsLast(&g_lstControlThreadsInactive, &pThread->Node);
|
---|
998 | int rc2 = VBoxServiceControlThreadWait(pThread, 30 * 1000 /* 30 seconds max. */,
|
---|
999 | NULL /* rc */);
|
---|
1000 | if (RT_SUCCESS(rc2))
|
---|
1001 | {
|
---|
1002 | RTListNodeRemove(&pThread->Node);
|
---|
1003 |
|
---|
1004 | rc2 = VBoxServiceControlThreadFree(pThread);
|
---|
1005 | if (RT_FAILURE(rc2))
|
---|
1006 | {
|
---|
1007 | VBoxServiceError("Freeing guest process thread failed with rc=%Rrc\n", rc2);
|
---|
1008 | if (RT_SUCCESS(rc)) /* Keep original failure. */
|
---|
1009 | rc = rc2;
|
---|
1010 | }
|
---|
1011 | }
|
---|
1012 | else
|
---|
1013 | VBoxServiceError("Waiting on guest process thread failed with rc=%Rrc\n", rc2);
|
---|
1014 | /* Keep going. */
|
---|
1015 |
|
---|
1016 | if (fLast)
|
---|
1017 | break;
|
---|
1018 |
|
---|
1019 | pThread = pNext;
|
---|
1020 | }
|
---|
1021 |
|
---|
1022 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
1023 | if (RT_SUCCESS(rc))
|
---|
1024 | rc = rc2;
|
---|
1025 | }
|
---|
1026 |
|
---|
1027 | VBoxServiceVerbose(4, "Reaping threads returned with rc=%Rrc\n", rc);
|
---|
1028 | return rc;
|
---|
1029 | }
|
---|
1030 |
|
---|
1031 |
|
---|
1032 | /**
|
---|
1033 | * Destroys all guest process threads which are still active.
|
---|
1034 | */
|
---|
1035 | static void VBoxServiceControlShutdown(void)
|
---|
1036 | {
|
---|
1037 | VBoxServiceVerbose(2, "Shutting down ...\n");
|
---|
1038 |
|
---|
1039 | /* Signal all threads in the active list that we want to shutdown. */
|
---|
1040 | PVBOXSERVICECTRLTHREAD pThread;
|
---|
1041 | RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
|
---|
1042 | VBoxServiceControlThreadStop(pThread);
|
---|
1043 |
|
---|
1044 | /* Wait for all active threads to shutdown and destroy the active thread list. */
|
---|
1045 | pThread = RTListGetFirst(&g_lstControlThreadsActive, VBOXSERVICECTRLTHREAD, Node);
|
---|
1046 | while (pThread)
|
---|
1047 | {
|
---|
1048 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
1049 | bool fLast = RTListNodeIsLast(&g_lstControlThreadsActive, &pThread->Node);
|
---|
1050 |
|
---|
1051 | int rc2 = VBoxServiceControlThreadWait(pThread,
|
---|
1052 | 30 * 1000 /* Wait 30 seconds max. */,
|
---|
1053 | NULL /* rc */);
|
---|
1054 | if (RT_FAILURE(rc2))
|
---|
1055 | VBoxServiceError("Guest process thread failed to stop; rc=%Rrc\n", rc2);
|
---|
1056 |
|
---|
1057 | if (fLast)
|
---|
1058 | break;
|
---|
1059 |
|
---|
1060 | pThread = pNext;
|
---|
1061 | }
|
---|
1062 |
|
---|
1063 | int rc2 = VBoxServiceControlReapThreads();
|
---|
1064 | if (RT_FAILURE(rc2))
|
---|
1065 | VBoxServiceError("Reaping inactive threads failed with rc=%Rrc\n", rc2);
|
---|
1066 |
|
---|
1067 | AssertMsg(RTListIsEmpty(&g_lstControlThreadsActive),
|
---|
1068 | ("Guest process active thread list still contains entries when it should not\n"));
|
---|
1069 | AssertMsg(RTListIsEmpty(&g_lstControlThreadsInactive),
|
---|
1070 | ("Guest process inactive thread list still contains entries when it should not\n"));
|
---|
1071 |
|
---|
1072 | /* Destroy critical section. */
|
---|
1073 | RTCritSectDelete(&g_csControlThreads);
|
---|
1074 |
|
---|
1075 | /* Close all left guest files. */
|
---|
1076 | PVBOXSERVICECTRLFILE pFile;
|
---|
1077 | pFile = RTListGetFirst(&g_lstControlFiles, VBOXSERVICECTRLFILE, Node);
|
---|
1078 | while (pFile)
|
---|
1079 | {
|
---|
1080 | PVBOXSERVICECTRLFILE pNext = RTListNodeGetNext(&pFile->Node, VBOXSERVICECTRLFILE, Node);
|
---|
1081 | bool fLast = RTListNodeIsLast(&g_lstControlFiles, &pFile->Node);
|
---|
1082 |
|
---|
1083 | rc2 = RTFileClose(pFile->hFile);
|
---|
1084 | if (RT_FAILURE(rc2))
|
---|
1085 | {
|
---|
1086 | VBoxServiceError("Unable to close file \"%s\"; rc=%Rrc\n",
|
---|
1087 | pFile->szName, rc2);
|
---|
1088 | /* Keep going. */
|
---|
1089 | }
|
---|
1090 |
|
---|
1091 | RTListNodeRemove(&pFile->Node);
|
---|
1092 |
|
---|
1093 | if (fLast)
|
---|
1094 | break;
|
---|
1095 |
|
---|
1096 | pFile = pNext;
|
---|
1097 | }
|
---|
1098 |
|
---|
1099 | AssertMsg(RTListIsEmpty(&g_lstControlFiles),
|
---|
1100 | ("Guest file list still contains entries when it should not\n"));
|
---|
1101 |
|
---|
1102 | VBoxServiceVerbose(2, "Shutting down complete\n");
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 |
|
---|
1106 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
1107 | static DECLCALLBACK(void) VBoxServiceControlTerm(void)
|
---|
1108 | {
|
---|
1109 | VBoxServiceVerbose(3, "Terminating ...\n");
|
---|
1110 |
|
---|
1111 | VBoxServiceControlShutdown();
|
---|
1112 |
|
---|
1113 | VBoxServiceVerbose(3, "Disconnecting client ID=%u ...\n",
|
---|
1114 | g_uControlSvcClientID);
|
---|
1115 | VbglR3GuestCtrlDisconnect(g_uControlSvcClientID);
|
---|
1116 | g_uControlSvcClientID = 0;
|
---|
1117 |
|
---|
1118 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
1119 | {
|
---|
1120 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
1121 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
1122 | }
|
---|
1123 | }
|
---|
1124 |
|
---|
1125 |
|
---|
1126 | /**
|
---|
1127 | * Determines whether starting a new guest process according to the
|
---|
1128 | * maximum number of concurrent guest processes defined is allowed or not.
|
---|
1129 | *
|
---|
1130 | * @return IPRT status code.
|
---|
1131 | * @param pbAllowed True if starting (another) guest process
|
---|
1132 | * is allowed, false if not.
|
---|
1133 | */
|
---|
1134 | static int VBoxServiceControlStartAllowed(bool *pbAllowed)
|
---|
1135 | {
|
---|
1136 | AssertPtrReturn(pbAllowed, VERR_INVALID_POINTER);
|
---|
1137 |
|
---|
1138 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
1139 | if (RT_SUCCESS(rc))
|
---|
1140 | {
|
---|
1141 | /*
|
---|
1142 | * Check if we're respecting our memory policy by checking
|
---|
1143 | * how many guest processes are started and served already.
|
---|
1144 | */
|
---|
1145 | bool fLimitReached = false;
|
---|
1146 | if (g_uControlProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
|
---|
1147 | {
|
---|
1148 | uint32_t uProcsRunning = 0;
|
---|
1149 | PVBOXSERVICECTRLTHREAD pThread;
|
---|
1150 | RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
|
---|
1151 | uProcsRunning++;
|
---|
1152 |
|
---|
1153 | VBoxServiceVerbose(3, "Maximum served guest processes set to %u, running=%u\n",
|
---|
1154 | g_uControlProcsMaxKept, uProcsRunning);
|
---|
1155 |
|
---|
1156 | int32_t iProcsLeft = (g_uControlProcsMaxKept - uProcsRunning - 1);
|
---|
1157 | if (iProcsLeft < 0)
|
---|
1158 | {
|
---|
1159 | VBoxServiceVerbose(3, "Maximum running guest processes reached (%u)\n",
|
---|
1160 | g_uControlProcsMaxKept);
|
---|
1161 | fLimitReached = true;
|
---|
1162 | }
|
---|
1163 | }
|
---|
1164 |
|
---|
1165 | *pbAllowed = !fLimitReached;
|
---|
1166 |
|
---|
1167 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
1168 | if (RT_SUCCESS(rc))
|
---|
1169 | rc = rc2;
|
---|
1170 | }
|
---|
1171 |
|
---|
1172 | return rc;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 |
|
---|
1176 | /**
|
---|
1177 | * Finds a (formerly) started process given by its PID and locks it. Must be unlocked
|
---|
1178 | * by the caller with VBoxServiceControlThreadUnlock().
|
---|
1179 | *
|
---|
1180 | * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
|
---|
1181 | * @param uPID PID to search for.
|
---|
1182 | */
|
---|
1183 | PVBOXSERVICECTRLTHREAD VBoxServiceControlLockThread(uint32_t uPID)
|
---|
1184 | {
|
---|
1185 | PVBOXSERVICECTRLTHREAD pThread = NULL;
|
---|
1186 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
1187 | if (RT_SUCCESS(rc))
|
---|
1188 | {
|
---|
1189 | PVBOXSERVICECTRLTHREAD pThreadCur;
|
---|
1190 | RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
|
---|
1191 | {
|
---|
1192 | if (pThreadCur->uPID == uPID)
|
---|
1193 | {
|
---|
1194 | rc = RTCritSectEnter(&pThreadCur->CritSect);
|
---|
1195 | if (RT_SUCCESS(rc))
|
---|
1196 | pThread = pThreadCur;
|
---|
1197 | break;
|
---|
1198 | }
|
---|
1199 | }
|
---|
1200 |
|
---|
1201 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
1202 | if (RT_SUCCESS(rc))
|
---|
1203 | rc = rc2;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 | return pThread;
|
---|
1207 | }
|
---|
1208 |
|
---|
1209 |
|
---|
1210 | /**
|
---|
1211 | * Unlocks a previously locked guest process thread.
|
---|
1212 | *
|
---|
1213 | * @param pThread Thread to unlock.
|
---|
1214 | */
|
---|
1215 | void VBoxServiceControlUnlockThread(const PVBOXSERVICECTRLTHREAD pThread)
|
---|
1216 | {
|
---|
1217 | AssertPtr(pThread);
|
---|
1218 |
|
---|
1219 | int rc = RTCritSectLeave(&pThread->CritSect);
|
---|
1220 | AssertRC(rc);
|
---|
1221 | }
|
---|
1222 |
|
---|
1223 |
|
---|
1224 | /**
|
---|
1225 | * Assigns a valid PID to a guest control thread and also checks if there already was
|
---|
1226 | * another (stale) guest process which was using that PID before and destroys it.
|
---|
1227 | *
|
---|
1228 | * @return IPRT status code.
|
---|
1229 | * @param pThread Thread to assign PID to.
|
---|
1230 | * @param uPID PID to assign to the specified guest control execution thread.
|
---|
1231 | */
|
---|
1232 | int VBoxServiceControlAssignPID(PVBOXSERVICECTRLTHREAD pThread, uint32_t uPID)
|
---|
1233 | {
|
---|
1234 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
1235 | AssertReturn(uPID, VERR_INVALID_PARAMETER);
|
---|
1236 |
|
---|
1237 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
1238 | if (RT_SUCCESS(rc))
|
---|
1239 | {
|
---|
1240 | /* Search old threads using the desired PID and shut them down completely -- it's
|
---|
1241 | * not used anymore. */
|
---|
1242 | PVBOXSERVICECTRLTHREAD pThreadCur;
|
---|
1243 | bool fTryAgain = false;
|
---|
1244 | do
|
---|
1245 | {
|
---|
1246 | RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
|
---|
1247 | {
|
---|
1248 | if (pThreadCur->uPID == uPID)
|
---|
1249 | {
|
---|
1250 | Assert(pThreadCur != pThread); /* can't happen */
|
---|
1251 | uint32_t uTriedPID = uPID;
|
---|
1252 | uPID += 391939;
|
---|
1253 | VBoxServiceVerbose(2, "PID %u was used before, trying again with %u ...\n",
|
---|
1254 | uTriedPID, uPID);
|
---|
1255 | fTryAgain = true;
|
---|
1256 | break;
|
---|
1257 | }
|
---|
1258 | }
|
---|
1259 | } while (fTryAgain);
|
---|
1260 |
|
---|
1261 | /* Assign PID to current thread. */
|
---|
1262 | pThread->uPID = uPID;
|
---|
1263 |
|
---|
1264 | rc = RTCritSectLeave(&g_csControlThreads);
|
---|
1265 | AssertRC(rc);
|
---|
1266 | }
|
---|
1267 |
|
---|
1268 | return rc;
|
---|
1269 | }
|
---|
1270 |
|
---|
1271 |
|
---|
1272 | /**
|
---|
1273 | * The 'vminfo' service description.
|
---|
1274 | */
|
---|
1275 | VBOXSERVICE g_Control =
|
---|
1276 | {
|
---|
1277 | /* pszName. */
|
---|
1278 | "control",
|
---|
1279 | /* pszDescription. */
|
---|
1280 | "Host-driven Guest Control",
|
---|
1281 | /* pszUsage. */
|
---|
1282 | #ifdef DEBUG
|
---|
1283 | " [--control-dump-stderr] [--control-dump-stdout]\n"
|
---|
1284 | #endif
|
---|
1285 | " [--control-interval <ms>] [--control-procs-max-kept <x>]\n"
|
---|
1286 | " [--control-procs-mem-std[in|out|err] <KB>]"
|
---|
1287 | ,
|
---|
1288 | /* pszOptions. */
|
---|
1289 | #ifdef DEBUG
|
---|
1290 | " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
|
---|
1291 | " temporary directory.\n"
|
---|
1292 | " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
|
---|
1293 | " temporary directory.\n"
|
---|
1294 | #endif
|
---|
1295 | " --control-interval Specifies the interval at which to check for\n"
|
---|
1296 | " new control commands. The default is 1000 ms.\n"
|
---|
1297 | " --control-procs-max-kept\n"
|
---|
1298 | " Specifies how many started guest processes are\n"
|
---|
1299 | " kept into memory to work with. Default is 256.\n"
|
---|
1300 | ,
|
---|
1301 | /* methods */
|
---|
1302 | VBoxServiceControlPreInit,
|
---|
1303 | VBoxServiceControlOption,
|
---|
1304 | VBoxServiceControlInit,
|
---|
1305 | VBoxServiceControlWorker,
|
---|
1306 | VBoxServiceControlStop,
|
---|
1307 | VBoxServiceControlTerm
|
---|
1308 | };
|
---|
1309 |
|
---|