1 | /* $Id: VBoxServiceControl.cpp 40529 2012-03-19 11:11:18Z 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 25 processes. If 0 is specified,
|
---|
48 | * the maximum number of processes is unlimited. */
|
---|
49 | static uint32_t g_uControlProcsMaxKept = 25;
|
---|
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 |
|
---|
61 |
|
---|
62 | /*******************************************************************************
|
---|
63 | * Internal Functions *
|
---|
64 | *******************************************************************************/
|
---|
65 | /** @todo Shorten "VBoxServiceControl" to "gstsvcCntl". */
|
---|
66 | static int VBoxServiceControlReapThreads(void);
|
---|
67 | static int VBoxServiceControlStartAllowed(bool *pbAllowed);
|
---|
68 | static int VBoxServiceControlHandleCmdStartProc(uint32_t u32ClientId, uint32_t uNumParms);
|
---|
69 | static int VBoxServiceControlHandleCmdSetInput(uint32_t u32ClientId, uint32_t uNumParms, size_t cbMaxBufSize);
|
---|
70 | static int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms);
|
---|
71 |
|
---|
72 |
|
---|
73 | #ifdef DEBUG
|
---|
74 | static int vboxServiceControlDump(const char *pszFileName, void *pvBuf, size_t cbBuf)
|
---|
75 | {
|
---|
76 | AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
|
---|
77 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
78 |
|
---|
79 | if (!cbBuf)
|
---|
80 | return VINF_SUCCESS;
|
---|
81 |
|
---|
82 | char szFile[RTPATH_MAX];
|
---|
83 |
|
---|
84 | int rc = RTPathTemp(szFile, sizeof(szFile));
|
---|
85 | if (RT_SUCCESS(rc))
|
---|
86 | rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
|
---|
87 |
|
---|
88 | if (RT_SUCCESS(rc))
|
---|
89 | {
|
---|
90 | VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
|
---|
91 |
|
---|
92 | RTFILE fh;
|
---|
93 | rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
|
---|
94 | if (RT_SUCCESS(rc))
|
---|
95 | {
|
---|
96 | rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
|
---|
97 | RTFileClose(fh);
|
---|
98 | }
|
---|
99 | }
|
---|
100 |
|
---|
101 | return rc;
|
---|
102 | }
|
---|
103 | #endif
|
---|
104 |
|
---|
105 |
|
---|
106 | /** @copydoc VBOXSERVICE::pfnPreInit */
|
---|
107 | static DECLCALLBACK(int) VBoxServiceControlPreInit(void)
|
---|
108 | {
|
---|
109 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
110 | /*
|
---|
111 | * Read the service options from the VM's guest properties.
|
---|
112 | * Note that these options can be overridden by the command line options later.
|
---|
113 | */
|
---|
114 | uint32_t uGuestPropSvcClientID;
|
---|
115 | int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
|
---|
116 | if (RT_FAILURE(rc))
|
---|
117 | {
|
---|
118 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
119 | {
|
---|
120 | VBoxServiceVerbose(0, "Guest property service is not available, skipping\n");
|
---|
121 | rc = VINF_SUCCESS;
|
---|
122 | }
|
---|
123 | else
|
---|
124 | VBoxServiceError("Failed to connect to the guest property service! Error: %Rrc\n", rc);
|
---|
125 | }
|
---|
126 | else
|
---|
127 | {
|
---|
128 | rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--control-procs-max-kept",
|
---|
129 | &g_uControlProcsMaxKept, 0, UINT32_MAX - 1);
|
---|
130 |
|
---|
131 | VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
|
---|
132 | }
|
---|
133 |
|
---|
134 | if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
|
---|
135 | rc = VINF_SUCCESS;
|
---|
136 | return rc;
|
---|
137 | #else
|
---|
138 | /* Nothing to do here yet. */
|
---|
139 | return VINF_SUCCESS;
|
---|
140 | #endif
|
---|
141 | }
|
---|
142 |
|
---|
143 |
|
---|
144 | /** @copydoc VBOXSERVICE::pfnOption */
|
---|
145 | static DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
|
---|
146 | {
|
---|
147 | int rc = -1;
|
---|
148 | if (ppszShort)
|
---|
149 | /* no short options */;
|
---|
150 | else if (!strcmp(argv[*pi], "--control-interval"))
|
---|
151 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
152 | &g_uControlIntervalMS, 1, UINT32_MAX - 1);
|
---|
153 | else if (!strcmp(argv[*pi], "--control-procs-max-kept"))
|
---|
154 | rc = VBoxServiceArgUInt32(argc, argv, "", pi,
|
---|
155 | &g_uControlProcsMaxKept, 0, UINT32_MAX - 1);
|
---|
156 | #ifdef DEBUG
|
---|
157 | else if (!strcmp(argv[*pi], "--control-dump-stderr"))
|
---|
158 | {
|
---|
159 | g_fControlDumpStdErr = true;
|
---|
160 | rc = 0; /* Flag this command as parsed. */
|
---|
161 | }
|
---|
162 | else if (!strcmp(argv[*pi], "--control-dump-stdout"))
|
---|
163 | {
|
---|
164 | g_fControlDumpStdOut = true;
|
---|
165 | rc = 0; /* Flag this command as parsed. */
|
---|
166 | }
|
---|
167 | #endif
|
---|
168 | return rc;
|
---|
169 | }
|
---|
170 |
|
---|
171 |
|
---|
172 | /** @copydoc VBOXSERVICE::pfnInit */
|
---|
173 | static DECLCALLBACK(int) VBoxServiceControlInit(void)
|
---|
174 | {
|
---|
175 | /*
|
---|
176 | * If not specified, find the right interval default.
|
---|
177 | * Then create the event sem to block on.
|
---|
178 | */
|
---|
179 | if (!g_uControlIntervalMS)
|
---|
180 | g_uControlIntervalMS = 1000;
|
---|
181 |
|
---|
182 | int rc = RTSemEventMultiCreate(&g_hControlEvent);
|
---|
183 | AssertRCReturn(rc, rc);
|
---|
184 |
|
---|
185 | rc = VbglR3GuestCtrlConnect(&g_uControlSvcClientID);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | VBoxServiceVerbose(3, "Service client ID: %#x\n", g_uControlSvcClientID);
|
---|
189 |
|
---|
190 | /* Init thread lists. */
|
---|
191 | RTListInit(&g_lstControlThreadsActive);
|
---|
192 | RTListInit(&g_lstControlThreadsInactive);
|
---|
193 |
|
---|
194 | /* Init critical section for protecting the thread lists. */
|
---|
195 | rc = RTCritSectInit(&g_csControlThreads);
|
---|
196 | AssertRC(rc);
|
---|
197 | }
|
---|
198 | else
|
---|
199 | {
|
---|
200 | /* If the service was not found, we disable this service without
|
---|
201 | causing VBoxService to fail. */
|
---|
202 | if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
|
---|
203 | {
|
---|
204 | VBoxServiceVerbose(0, "Guest control service is not available\n");
|
---|
205 | rc = VERR_SERVICE_DISABLED;
|
---|
206 | }
|
---|
207 | else
|
---|
208 | VBoxServiceError("Failed to connect to the guest control service! Error: %Rrc\n", rc);
|
---|
209 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
210 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
211 | }
|
---|
212 | return rc;
|
---|
213 | }
|
---|
214 |
|
---|
215 |
|
---|
216 | /** @copydoc VBOXSERVICE::pfnWorker */
|
---|
217 | DECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
|
---|
218 | {
|
---|
219 | /*
|
---|
220 | * Tell the control thread that it can continue
|
---|
221 | * spawning services.
|
---|
222 | */
|
---|
223 | RTThreadUserSignal(RTThreadSelf());
|
---|
224 | Assert(g_uControlSvcClientID > 0);
|
---|
225 |
|
---|
226 | int rc = VINF_SUCCESS;
|
---|
227 |
|
---|
228 | /*
|
---|
229 | * Execution loop.
|
---|
230 | *
|
---|
231 | * @todo
|
---|
232 | */
|
---|
233 | for (;;)
|
---|
234 | {
|
---|
235 | VBoxServiceVerbose(3, "Waiting for host msg ...\n");
|
---|
236 | uint32_t uMsg;
|
---|
237 | uint32_t cParms;
|
---|
238 | rc = VbglR3GuestCtrlWaitForHostMsg(g_uControlSvcClientID, &uMsg, &cParms);
|
---|
239 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
240 | {
|
---|
241 | VBoxServiceVerbose(4, "Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
|
---|
242 | rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
|
---|
243 | }
|
---|
244 | else if (RT_FAILURE(rc))
|
---|
245 | VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
|
---|
246 | if (RT_SUCCESS(rc))
|
---|
247 | {
|
---|
248 | VBoxServiceVerbose(3, "Msg=%u (%u parms) retrieved\n", uMsg, cParms);
|
---|
249 | switch (uMsg)
|
---|
250 | {
|
---|
251 | case HOST_CANCEL_PENDING_WAITS:
|
---|
252 | VBoxServiceVerbose(3, "Host asked us to quit ...\n");
|
---|
253 | break;
|
---|
254 |
|
---|
255 | case HOST_EXEC_CMD:
|
---|
256 | rc = VBoxServiceControlHandleCmdStartProc(g_uControlSvcClientID, cParms);
|
---|
257 | break;
|
---|
258 |
|
---|
259 | case HOST_EXEC_SET_INPUT:
|
---|
260 | /** @todo Make buffer size configurable via guest properties/argv! */
|
---|
261 | rc = VBoxServiceControlHandleCmdSetInput(g_uControlSvcClientID, cParms, _1M /* Buffer size */);
|
---|
262 | break;
|
---|
263 |
|
---|
264 | case HOST_EXEC_GET_OUTPUT:
|
---|
265 | rc = VBoxServiceControlHandleCmdGetOutput(g_uControlSvcClientID, cParms);
|
---|
266 | break;
|
---|
267 |
|
---|
268 | default:
|
---|
269 | VBoxServiceVerbose(3, "Unsupported message from host! Msg=%u\n", uMsg);
|
---|
270 | /* Don't terminate here; just wait for the next message. */
|
---|
271 | break;
|
---|
272 | }
|
---|
273 | }
|
---|
274 |
|
---|
275 | /* Do we need to shutdown? */
|
---|
276 | if ( *pfShutdown
|
---|
277 | || uMsg == HOST_CANCEL_PENDING_WAITS)
|
---|
278 | {
|
---|
279 | rc = VINF_SUCCESS;
|
---|
280 | break;
|
---|
281 | }
|
---|
282 |
|
---|
283 | /* Let's sleep for a bit and let others run ... */
|
---|
284 | RTThreadYield();
|
---|
285 | }
|
---|
286 |
|
---|
287 | return rc;
|
---|
288 | }
|
---|
289 |
|
---|
290 |
|
---|
291 | /**
|
---|
292 | * Handles starting processes on the guest.
|
---|
293 | *
|
---|
294 | * @returns IPRT status code.
|
---|
295 | * @param uClientID The HGCM client session ID.
|
---|
296 | * @param cParms The number of parameters the host is offering.
|
---|
297 | */
|
---|
298 | static int VBoxServiceControlHandleCmdStartProc(uint32_t uClientID, uint32_t cParms)
|
---|
299 | {
|
---|
300 | uint32_t uContextID = 0;
|
---|
301 |
|
---|
302 | int rc;
|
---|
303 | bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
|
---|
304 | if (cParms == 11)
|
---|
305 | {
|
---|
306 | VBOXSERVICECTRLPROCESS proc;
|
---|
307 | RT_ZERO(proc);
|
---|
308 |
|
---|
309 | rc = VbglR3GuestCtrlExecGetHostCmdExec(uClientID,
|
---|
310 | cParms,
|
---|
311 | &uContextID,
|
---|
312 | /* Command */
|
---|
313 | proc.szCmd, sizeof(proc.szCmd),
|
---|
314 | /* Flags */
|
---|
315 | &proc.uFlags,
|
---|
316 | /* Arguments */
|
---|
317 | proc.szArgs, sizeof(proc.szArgs), &proc.uNumArgs,
|
---|
318 | /* Environment */
|
---|
319 | proc.szEnv, &proc.cbEnv, &proc.uNumEnvVars,
|
---|
320 | /* Credentials */
|
---|
321 | proc.szUser, sizeof(proc.szUser),
|
---|
322 | proc.szPassword, sizeof(proc.szPassword),
|
---|
323 | /* Timelimit */
|
---|
324 | &proc.uTimeLimitMS);
|
---|
325 | if (RT_SUCCESS(rc))
|
---|
326 | {
|
---|
327 | VBoxServiceVerbose(3, "Request to start process szCmd=%s, uFlags=0x%x, szArgs=%s, szEnv=%s, szUser=%s, uTimeout=%u\n",
|
---|
328 | proc.szCmd, proc.uFlags,
|
---|
329 | proc.uNumArgs ? proc.szArgs : "<None>",
|
---|
330 | proc.uNumEnvVars ? proc.szEnv : "<None>",
|
---|
331 | proc.szUser, proc.uTimeLimitMS);
|
---|
332 |
|
---|
333 | rc = VBoxServiceControlReapThreads();
|
---|
334 | if (RT_FAILURE(rc))
|
---|
335 | VBoxServiceError("Reaping stopped processes failed with rc=%Rrc\n", rc);
|
---|
336 | /* Keep going. */
|
---|
337 |
|
---|
338 | rc = VBoxServiceControlStartAllowed(&fStartAllowed);
|
---|
339 | if (RT_SUCCESS(rc))
|
---|
340 | {
|
---|
341 | if (fStartAllowed)
|
---|
342 | {
|
---|
343 | rc = VBoxServiceControlThreadStart(uContextID, &proc);
|
---|
344 | }
|
---|
345 | else
|
---|
346 | rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
|
---|
347 | }
|
---|
348 | }
|
---|
349 | }
|
---|
350 | else
|
---|
351 | rc = VERR_INVALID_PARAMETER; /* Incorrect number of parameters. */
|
---|
352 |
|
---|
353 | /* In case of an error we need to notify the host to not wait forever for our response. */
|
---|
354 | if (RT_FAILURE(rc))
|
---|
355 | {
|
---|
356 | VBoxServiceError("Starting process failed with rc=%Rrc\n", rc);
|
---|
357 |
|
---|
358 | int rc2 = VbglR3GuestCtrlExecReportStatus(uClientID, uContextID, 0 /* PID, invalid. */,
|
---|
359 | PROC_STS_ERROR, rc,
|
---|
360 | NULL /* pvData */, 0 /* cbData */);
|
---|
361 | if (RT_FAILURE(rc2))
|
---|
362 | {
|
---|
363 | VBoxServiceError("Error sending start process status to host, rc=%Rrc\n", rc2);
|
---|
364 | if (RT_SUCCESS(rc))
|
---|
365 | rc = rc2;
|
---|
366 | }
|
---|
367 | }
|
---|
368 |
|
---|
369 | return rc;
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Gets output from stdout/stderr of a specified guest process.
|
---|
375 | *
|
---|
376 | * @return IPRT status code.
|
---|
377 | * @param uPID PID of process to retrieve the output from.
|
---|
378 | * @param uHandleId Stream ID (stdout = 0, stderr = 2) to get the output from.
|
---|
379 | * @param uTimeout Timeout (in ms) to wait for output becoming available.
|
---|
380 | * @param pvBuf Pointer to a pre-allocated buffer to store the output.
|
---|
381 | * @param cbBuf Size (in bytes) of the pre-allocated buffer.
|
---|
382 | * @param pcbRead Pointer to number of bytes read. Optional.
|
---|
383 | */
|
---|
384 | int VBoxServiceControlExecGetOutput(uint32_t uPID, uint32_t uCID,
|
---|
385 | uint32_t uHandleId, uint32_t uTimeout,
|
---|
386 | void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
|
---|
387 | {
|
---|
388 | AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
|
---|
389 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
390 | /* pcbRead is optional. */
|
---|
391 |
|
---|
392 | int rc = VINF_SUCCESS;
|
---|
393 | VBOXSERVICECTRLREQUESTTYPE reqType;
|
---|
394 | switch (uHandleId)
|
---|
395 | {
|
---|
396 | case OUTPUT_HANDLE_ID_STDERR:
|
---|
397 | reqType = VBOXSERVICECTRLREQUEST_STDERR_READ;
|
---|
398 | break;
|
---|
399 |
|
---|
400 | case OUTPUT_HANDLE_ID_STDOUT:
|
---|
401 | case OUTPUT_HANDLE_ID_STDOUT_DEPRECATED:
|
---|
402 | reqType = VBOXSERVICECTRLREQUEST_STDOUT_READ;
|
---|
403 | break;
|
---|
404 |
|
---|
405 | default:
|
---|
406 | rc = VERR_INVALID_PARAMETER;
|
---|
407 | break;
|
---|
408 | }
|
---|
409 |
|
---|
410 | PVBOXSERVICECTRLREQUEST pRequest;
|
---|
411 | if (RT_SUCCESS(rc))
|
---|
412 | {
|
---|
413 | rc = VBoxServiceControlThreadRequestAllocEx(&pRequest, reqType,
|
---|
414 | pvBuf, cbBuf, uCID);
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | rc = VBoxServiceControlThreadPerform(uPID, pRequest);
|
---|
417 |
|
---|
418 | if (RT_SUCCESS(rc))
|
---|
419 | {
|
---|
420 | if (pcbRead)
|
---|
421 | *pcbRead = pRequest->cbData;
|
---|
422 | }
|
---|
423 |
|
---|
424 | VBoxServiceControlThreadRequestFree(pRequest);
|
---|
425 | }
|
---|
426 |
|
---|
427 | return rc;
|
---|
428 | }
|
---|
429 |
|
---|
430 |
|
---|
431 | /**
|
---|
432 | * Sets the specified guest thread to a certain list.
|
---|
433 | *
|
---|
434 | * @return IPRT status code.
|
---|
435 | * @param enmList List to move thread to.
|
---|
436 | * @param pThread Thread to set inactive.
|
---|
437 | */
|
---|
438 | int VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLISTTYPE enmList,
|
---|
439 | PVBOXSERVICECTRLTHREAD pThread)
|
---|
440 | {
|
---|
441 | AssertReturn(enmList > VBOXSERVICECTRLTHREADLIST_UNKNOWN, VERR_INVALID_PARAMETER);
|
---|
442 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
443 |
|
---|
444 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
445 | if (RT_SUCCESS(rc))
|
---|
446 | {
|
---|
447 | VBoxServiceVerbose(3, "Setting thread (PID %u) to list %d\n",
|
---|
448 | pThread->uPID, enmList);
|
---|
449 |
|
---|
450 | PRTLISTANCHOR pAnchor = NULL;
|
---|
451 | switch (enmList)
|
---|
452 | {
|
---|
453 | case VBOXSERVICECTRLTHREADLIST_STOPPED:
|
---|
454 | pAnchor = &g_lstControlThreadsInactive;
|
---|
455 | break;
|
---|
456 |
|
---|
457 | case VBOXSERVICECTRLTHREADLIST_RUNNING:
|
---|
458 | pAnchor = &g_lstControlThreadsActive;
|
---|
459 | break;
|
---|
460 |
|
---|
461 | default:
|
---|
462 | AssertMsgFailed(("Unknown list type: %u", enmList));
|
---|
463 | break;
|
---|
464 | }
|
---|
465 |
|
---|
466 | if (!pAnchor)
|
---|
467 | rc = VERR_INVALID_PARAMETER;
|
---|
468 |
|
---|
469 | if (RT_SUCCESS(rc))
|
---|
470 | {
|
---|
471 | if (pThread->pAnchor != NULL)
|
---|
472 | {
|
---|
473 | /* If thread was assigned to a list before,
|
---|
474 | * remove the thread from the old list first. */
|
---|
475 | /* rc = */ RTListNodeRemove(&pThread->Node);
|
---|
476 | }
|
---|
477 |
|
---|
478 | /* Add thread to desired list. */
|
---|
479 | /* rc = */ RTListAppend(pAnchor, &pThread->Node);
|
---|
480 | pThread->pAnchor = pAnchor;
|
---|
481 | }
|
---|
482 |
|
---|
483 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
484 | if (RT_SUCCESS(rc))
|
---|
485 | rc = rc2;
|
---|
486 | }
|
---|
487 |
|
---|
488 | return VINF_SUCCESS;
|
---|
489 | }
|
---|
490 |
|
---|
491 |
|
---|
492 | /**
|
---|
493 | * Injects input to a specified running process.
|
---|
494 | *
|
---|
495 | * @return IPRT status code.
|
---|
496 | * @param uPID PID of process to set the input for.
|
---|
497 | * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
|
---|
498 | * @param pvBuf Pointer to a buffer containing the actual input data.
|
---|
499 | * @param cbBuf Size (in bytes) of the input buffer data.
|
---|
500 | * @param pcbWritten Pointer to number of bytes written to the process. Optional.
|
---|
501 | */
|
---|
502 | int VBoxServiceControlSetInput(uint32_t uPID, uint32_t uCID,
|
---|
503 | bool fPendingClose,
|
---|
504 | void *pvBuf, uint32_t cbBuf,
|
---|
505 | uint32_t *pcbWritten)
|
---|
506 | {
|
---|
507 | /* pvBuf is optional. */
|
---|
508 | /* cbBuf is optional. */
|
---|
509 | /* pcbWritten is optional. */
|
---|
510 |
|
---|
511 | PVBOXSERVICECTRLREQUEST pRequest;
|
---|
512 | int rc = VBoxServiceControlThreadRequestAllocEx(&pRequest,
|
---|
513 | fPendingClose
|
---|
514 | ? VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF
|
---|
515 | : VBOXSERVICECTRLREQUEST_STDIN_WRITE,
|
---|
516 | pvBuf, cbBuf, uCID);
|
---|
517 | if (RT_SUCCESS(rc))
|
---|
518 | {
|
---|
519 | rc = VBoxServiceControlThreadPerform(uPID, pRequest);
|
---|
520 | if (RT_SUCCESS(rc))
|
---|
521 | {
|
---|
522 | if (pcbWritten)
|
---|
523 | *pcbWritten = pRequest->cbData;
|
---|
524 | }
|
---|
525 |
|
---|
526 | VBoxServiceControlThreadRequestFree(pRequest);
|
---|
527 | }
|
---|
528 |
|
---|
529 | return rc;
|
---|
530 | }
|
---|
531 |
|
---|
532 |
|
---|
533 | /**
|
---|
534 | * Handles input for a started process by copying the received data into its
|
---|
535 | * stdin pipe.
|
---|
536 | *
|
---|
537 | * @returns IPRT status code.
|
---|
538 | * @param idClient The HGCM client session ID.
|
---|
539 | * @param cParms The number of parameters the host is
|
---|
540 | * offering.
|
---|
541 | * @param cMaxBufSize The maximum buffer size for retrieving the input data.
|
---|
542 | */
|
---|
543 | static int VBoxServiceControlHandleCmdSetInput(uint32_t idClient, uint32_t cParms, size_t cbMaxBufSize)
|
---|
544 | {
|
---|
545 | uint32_t uContextID;
|
---|
546 | uint32_t uPID;
|
---|
547 | uint32_t uFlags;
|
---|
548 | uint32_t cbSize;
|
---|
549 |
|
---|
550 | AssertReturn(RT_IS_POWER_OF_TWO(cbMaxBufSize), VERR_INVALID_PARAMETER);
|
---|
551 | uint8_t *pabBuffer = (uint8_t*)RTMemAlloc(cbMaxBufSize);
|
---|
552 | AssertPtrReturn(pabBuffer, VERR_NO_MEMORY);
|
---|
553 |
|
---|
554 | uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
|
---|
555 | uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
|
---|
556 |
|
---|
557 | /*
|
---|
558 | * Ask the host for the input data.
|
---|
559 | */
|
---|
560 | int rc = VbglR3GuestCtrlExecGetHostCmdInput(idClient, cParms,
|
---|
561 | &uContextID, &uPID, &uFlags,
|
---|
562 | pabBuffer, cbMaxBufSize, &cbSize);
|
---|
563 | if (RT_FAILURE(rc))
|
---|
564 | {
|
---|
565 | VBoxServiceError("[PID %u]: Failed to retrieve exec input command! Error: %Rrc\n",
|
---|
566 | uPID, rc);
|
---|
567 | }
|
---|
568 | else if (cbSize > cbMaxBufSize)
|
---|
569 | {
|
---|
570 | VBoxServiceError("[PID %u]: Too much input received! cbSize=%u, cbMaxBufSize=%u\n",
|
---|
571 | uPID, cbSize, cbMaxBufSize);
|
---|
572 | rc = VERR_INVALID_PARAMETER;
|
---|
573 | }
|
---|
574 | else
|
---|
575 | {
|
---|
576 | /*
|
---|
577 | * Is this the last input block we need to deliver? Then let the pipe know ...
|
---|
578 | */
|
---|
579 | bool fPendingClose = false;
|
---|
580 | if (uFlags & INPUT_FLAG_EOF)
|
---|
581 | {
|
---|
582 | fPendingClose = true;
|
---|
583 | VBoxServiceVerbose(4, "[PID %u]: Got last input block of size %u ...\n",
|
---|
584 | uPID, cbSize);
|
---|
585 | }
|
---|
586 |
|
---|
587 | rc = VBoxServiceControlSetInput(uPID, uContextID, fPendingClose, pabBuffer,
|
---|
588 | cbSize, &cbWritten);
|
---|
589 | VBoxServiceVerbose(4, "[PID %u]: Written input, CID=%u, rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
|
---|
590 | uPID, uContextID, rc, uFlags, fPendingClose, cbSize, cbWritten);
|
---|
591 | if (RT_SUCCESS(rc))
|
---|
592 | {
|
---|
593 | if (cbWritten || !cbSize) /* Did we write something or was there anything to write at all? */
|
---|
594 | {
|
---|
595 | uStatus = INPUT_STS_WRITTEN;
|
---|
596 | uFlags = 0;
|
---|
597 | }
|
---|
598 | }
|
---|
599 | else
|
---|
600 | {
|
---|
601 | if (rc == VERR_BAD_PIPE)
|
---|
602 | uStatus = INPUT_STS_TERMINATED;
|
---|
603 | else if (rc == VERR_BUFFER_OVERFLOW)
|
---|
604 | uStatus = INPUT_STS_OVERFLOW;
|
---|
605 | }
|
---|
606 | }
|
---|
607 | RTMemFree(pabBuffer);
|
---|
608 |
|
---|
609 | /*
|
---|
610 | * If there was an error and we did not set the host status
|
---|
611 | * yet, then do it now.
|
---|
612 | */
|
---|
613 | if ( RT_FAILURE(rc)
|
---|
614 | && uStatus == INPUT_STS_UNDEFINED)
|
---|
615 | {
|
---|
616 | uStatus = INPUT_STS_ERROR;
|
---|
617 | uFlags = rc;
|
---|
618 | }
|
---|
619 | Assert(uStatus > INPUT_STS_UNDEFINED);
|
---|
620 |
|
---|
621 | VBoxServiceVerbose(3, "[PID %u]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
|
---|
622 | uPID, uContextID, uStatus, uFlags, cbWritten);
|
---|
623 |
|
---|
624 | /* Note: Since the context ID is unique the request *has* to be completed here,
|
---|
625 | * regardless whether we got data or not! Otherwise the progress object
|
---|
626 | * on the host never will get completed! */
|
---|
627 | rc = VbglR3GuestCtrlExecReportStatusIn(idClient, uContextID, uPID,
|
---|
628 | uStatus, uFlags, (uint32_t)cbWritten);
|
---|
629 |
|
---|
630 | if (RT_FAILURE(rc))
|
---|
631 | VBoxServiceError("[PID %u]: Failed to report input status! Error: %Rrc\n",
|
---|
632 | uPID, rc);
|
---|
633 | return rc;
|
---|
634 | }
|
---|
635 |
|
---|
636 |
|
---|
637 | /**
|
---|
638 | * Handles the guest control output command.
|
---|
639 | *
|
---|
640 | * @return IPRT status code.
|
---|
641 | * @param idClient The HGCM client session ID.
|
---|
642 | * @param cParms The number of parameters the host is offering.
|
---|
643 | */
|
---|
644 | static int VBoxServiceControlHandleCmdGetOutput(uint32_t idClient, uint32_t cParms)
|
---|
645 | {
|
---|
646 | uint32_t uContextID;
|
---|
647 | uint32_t uPID;
|
---|
648 | uint32_t uHandleID;
|
---|
649 | uint32_t uFlags;
|
---|
650 |
|
---|
651 | int rc = VbglR3GuestCtrlExecGetHostCmdOutput(idClient, cParms,
|
---|
652 | &uContextID, &uPID, &uHandleID, &uFlags);
|
---|
653 | if (RT_SUCCESS(rc))
|
---|
654 | {
|
---|
655 | uint8_t *pBuf = (uint8_t*)RTMemAlloc(_64K);
|
---|
656 | if (pBuf)
|
---|
657 | {
|
---|
658 | uint32_t cbRead = 0;
|
---|
659 | rc = VBoxServiceControlExecGetOutput(uPID, uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
|
---|
660 | pBuf, _64K /* cbSize */, &cbRead);
|
---|
661 | VBoxServiceVerbose(3, "[PID %u]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
|
---|
662 | uPID, rc, uContextID, cbRead, uHandleID, uFlags);
|
---|
663 |
|
---|
664 | #ifdef DEBUG
|
---|
665 | if ( g_fControlDumpStdErr
|
---|
666 | && uHandleID == OUTPUT_HANDLE_ID_STDERR)
|
---|
667 | {
|
---|
668 | char szPID[RTPATH_MAX];
|
---|
669 | if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
|
---|
670 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
671 | if (RT_SUCCESS(rc))
|
---|
672 | rc = vboxServiceControlDump(szPID, pBuf, cbRead);
|
---|
673 | }
|
---|
674 | else if ( g_fControlDumpStdOut
|
---|
675 | && ( uHandleID == OUTPUT_HANDLE_ID_STDOUT
|
---|
676 | || uHandleID == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED))
|
---|
677 | {
|
---|
678 | char szPID[RTPATH_MAX];
|
---|
679 | if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
|
---|
680 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
681 | if (RT_SUCCESS(rc))
|
---|
682 | rc = vboxServiceControlDump(szPID, pBuf, cbRead);
|
---|
683 | AssertRC(rc);
|
---|
684 | }
|
---|
685 | #endif
|
---|
686 | /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
|
---|
687 | * data which the host needs to work with -- so just pass through all data unfiltered! */
|
---|
688 |
|
---|
689 | /* Note: Since the context ID is unique the request *has* to be completed here,
|
---|
690 | * regardless whether we got data or not! Otherwise the progress object
|
---|
691 | * on the host never will get completed! */
|
---|
692 | int rc2 = VbglR3GuestCtrlExecSendOut(idClient, uContextID, uPID, uHandleID, uFlags,
|
---|
693 | pBuf, cbRead);
|
---|
694 | if (RT_SUCCESS(rc))
|
---|
695 | rc = rc2;
|
---|
696 | else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
|
---|
697 | rc = VINF_SUCCESS;
|
---|
698 |
|
---|
699 | RTMemFree(pBuf);
|
---|
700 | }
|
---|
701 | else
|
---|
702 | rc = VERR_NO_MEMORY;
|
---|
703 | }
|
---|
704 |
|
---|
705 | if (RT_FAILURE(rc))
|
---|
706 | VBoxServiceError("[PID %u]: Error handling output command! Error: %Rrc\n",
|
---|
707 | uPID, rc);
|
---|
708 | return rc;
|
---|
709 | }
|
---|
710 |
|
---|
711 |
|
---|
712 | /** @copydoc VBOXSERVICE::pfnStop */
|
---|
713 | static DECLCALLBACK(void) VBoxServiceControlStop(void)
|
---|
714 | {
|
---|
715 | VBoxServiceVerbose(3, "Stopping ...\n");
|
---|
716 |
|
---|
717 | /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
|
---|
718 | * annoying call since doesn't support timeouts in the posix world. */
|
---|
719 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
720 | RTSemEventMultiSignal(g_hControlEvent);
|
---|
721 |
|
---|
722 | /*
|
---|
723 | * Ask the host service to cancel all pending requests so that we can
|
---|
724 | * shutdown properly here.
|
---|
725 | */
|
---|
726 | if (g_uControlSvcClientID)
|
---|
727 | {
|
---|
728 | VBoxServiceVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
|
---|
729 | g_uControlSvcClientID);
|
---|
730 |
|
---|
731 | int rc = VbglR3GuestCtrlCancelPendingWaits(g_uControlSvcClientID);
|
---|
732 | if (RT_FAILURE(rc))
|
---|
733 | VBoxServiceError("Cancelling pending waits failed; rc=%Rrc\n", rc);
|
---|
734 | }
|
---|
735 | }
|
---|
736 |
|
---|
737 |
|
---|
738 | /**
|
---|
739 | * Reaps all inactive guest process threads.
|
---|
740 | *
|
---|
741 | * @return IPRT status code.
|
---|
742 | */
|
---|
743 | static int VBoxServiceControlReapThreads(void)
|
---|
744 | {
|
---|
745 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
746 | if (RT_SUCCESS(rc))
|
---|
747 | {
|
---|
748 | PVBOXSERVICECTRLTHREAD pThread =
|
---|
749 | RTListGetFirst(&g_lstControlThreadsInactive, VBOXSERVICECTRLTHREAD, Node);
|
---|
750 | while (pThread)
|
---|
751 | {
|
---|
752 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
753 | bool fLast = RTListNodeIsLast(&g_lstControlThreadsInactive, &pThread->Node);
|
---|
754 |
|
---|
755 | int rc2 = VBoxServiceControlThreadWait(pThread, 30 * 1000 /* 30 seconds max. */);
|
---|
756 | if (RT_SUCCESS(rc2))
|
---|
757 | {
|
---|
758 | RTListNodeRemove(&pThread->Node);
|
---|
759 |
|
---|
760 | rc2 = VBoxServiceControlThreadFree(pThread);
|
---|
761 | if (RT_FAILURE(rc2))
|
---|
762 | {
|
---|
763 | VBoxServiceError("Stopping guest process thread failed with rc=%Rrc\n", rc2);
|
---|
764 | if (RT_SUCCESS(rc)) /* Keep original failure. */
|
---|
765 | rc = rc2;
|
---|
766 | }
|
---|
767 | }
|
---|
768 | else
|
---|
769 | VBoxServiceError("Waiting on guest process thread failed with rc=%Rrc\n", rc2);
|
---|
770 | /* Keep going. */
|
---|
771 |
|
---|
772 | if (fLast)
|
---|
773 | break;
|
---|
774 |
|
---|
775 | pThread = pNext;
|
---|
776 | }
|
---|
777 |
|
---|
778 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
779 | if (RT_SUCCESS(rc))
|
---|
780 | rc = rc2;
|
---|
781 | }
|
---|
782 |
|
---|
783 | VBoxServiceVerbose(4, "Reaping threads returned with rc=%Rrc\n", rc);
|
---|
784 | return rc;
|
---|
785 | }
|
---|
786 |
|
---|
787 |
|
---|
788 | /**
|
---|
789 | * Destroys all guest process threads which are still active.
|
---|
790 | */
|
---|
791 | static void VBoxServiceControlShutdown(void)
|
---|
792 | {
|
---|
793 | VBoxServiceVerbose(2, "Shutting down ...\n");
|
---|
794 |
|
---|
795 | /* Signal all threads in the active list that we want to shutdown. */
|
---|
796 | PVBOXSERVICECTRLTHREAD pThread;
|
---|
797 | RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
|
---|
798 | VBoxServiceControlThreadStop(pThread);
|
---|
799 |
|
---|
800 | /* Wait for all active threads to shutdown and destroy the active thread list. */
|
---|
801 | pThread = RTListGetFirst(&g_lstControlThreadsActive, VBOXSERVICECTRLTHREAD, Node);
|
---|
802 | while (pThread)
|
---|
803 | {
|
---|
804 | PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
|
---|
805 | bool fLast = RTListNodeIsLast(&g_lstControlThreadsActive, &pThread->Node);
|
---|
806 |
|
---|
807 | int rc2 = VBoxServiceControlThreadWait(pThread,
|
---|
808 | 30 * 1000 /* Wait 30 seconds max. */);
|
---|
809 | if (RT_FAILURE(rc2))
|
---|
810 | VBoxServiceError("Guest process thread failed to stop; rc=%Rrc\n", rc2);
|
---|
811 |
|
---|
812 | if (fLast)
|
---|
813 | break;
|
---|
814 |
|
---|
815 | pThread = pNext;
|
---|
816 | }
|
---|
817 |
|
---|
818 | int rc2 = VBoxServiceControlReapThreads();
|
---|
819 | if (RT_FAILURE(rc2))
|
---|
820 | VBoxServiceError("Reaping inactive threads failed with rc=%Rrc\n", rc2);
|
---|
821 |
|
---|
822 | AssertMsg(RTListIsEmpty(&g_lstControlThreadsActive),
|
---|
823 | ("Guest process active thread list still contains entries when it should not\n"));
|
---|
824 | AssertMsg(RTListIsEmpty(&g_lstControlThreadsInactive),
|
---|
825 | ("Guest process inactive thread list still contains entries when it should not\n"));
|
---|
826 |
|
---|
827 | /* Destroy critical section. */
|
---|
828 | RTCritSectDelete(&g_csControlThreads);
|
---|
829 |
|
---|
830 | VBoxServiceVerbose(2, "Shutting down complete\n");
|
---|
831 | }
|
---|
832 |
|
---|
833 |
|
---|
834 | /** @copydoc VBOXSERVICE::pfnTerm */
|
---|
835 | static DECLCALLBACK(void) VBoxServiceControlTerm(void)
|
---|
836 | {
|
---|
837 | VBoxServiceVerbose(3, "Terminating ...\n");
|
---|
838 |
|
---|
839 | VBoxServiceControlShutdown();
|
---|
840 |
|
---|
841 | VBoxServiceVerbose(3, "Disconnecting client ID=%u ...\n",
|
---|
842 | g_uControlSvcClientID);
|
---|
843 | VbglR3GuestCtrlDisconnect(g_uControlSvcClientID);
|
---|
844 | g_uControlSvcClientID = 0;
|
---|
845 |
|
---|
846 | if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
|
---|
847 | {
|
---|
848 | RTSemEventMultiDestroy(g_hControlEvent);
|
---|
849 | g_hControlEvent = NIL_RTSEMEVENTMULTI;
|
---|
850 | }
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Determines whether starting a new guest process according to the
|
---|
856 | * maximum number of concurrent guest processes defined is allowed or not.
|
---|
857 | *
|
---|
858 | * @return IPRT status code.
|
---|
859 | * @param pbAllowed True if starting (another) guest process
|
---|
860 | * is allowed, false if not.
|
---|
861 | */
|
---|
862 | static int VBoxServiceControlStartAllowed(bool *pbAllowed)
|
---|
863 | {
|
---|
864 | AssertPtrReturn(pbAllowed, VERR_INVALID_POINTER);
|
---|
865 |
|
---|
866 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
867 | if (RT_SUCCESS(rc))
|
---|
868 | {
|
---|
869 | /*
|
---|
870 | * Check if we're respecting our memory policy by checking
|
---|
871 | * how many guest processes are started and served already.
|
---|
872 | */
|
---|
873 | bool fLimitReached = false;
|
---|
874 | if (g_uControlProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
|
---|
875 | {
|
---|
876 | uint32_t uProcsRunning = 0;
|
---|
877 | PVBOXSERVICECTRLTHREAD pThread;
|
---|
878 | RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
|
---|
879 | uProcsRunning++;
|
---|
880 |
|
---|
881 | VBoxServiceVerbose(3, "Maximum served guest processes set to %u, running=%u\n",
|
---|
882 | g_uControlProcsMaxKept, uProcsRunning);
|
---|
883 |
|
---|
884 | int32_t iProcsLeft = (g_uControlProcsMaxKept - uProcsRunning - 1);
|
---|
885 | if (iProcsLeft < 0)
|
---|
886 | {
|
---|
887 | VBoxServiceVerbose(3, "Maximum running guest processes reached (%u)\n",
|
---|
888 | g_uControlProcsMaxKept);
|
---|
889 | fLimitReached = true;
|
---|
890 | }
|
---|
891 | }
|
---|
892 |
|
---|
893 | *pbAllowed = !fLimitReached;
|
---|
894 |
|
---|
895 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
896 | if (RT_SUCCESS(rc))
|
---|
897 | rc = rc2;
|
---|
898 | }
|
---|
899 |
|
---|
900 | return rc;
|
---|
901 | }
|
---|
902 |
|
---|
903 |
|
---|
904 | /**
|
---|
905 | * Finds a (formerly) started process given by its PID and locks it. Must be unlocked
|
---|
906 | * by the caller with VBoxServiceControlThreadUnlock().
|
---|
907 | *
|
---|
908 | * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
|
---|
909 | * @param uPID PID to search for.
|
---|
910 | */
|
---|
911 | PVBOXSERVICECTRLTHREAD VBoxServiceControlLockThread(uint32_t uPID)
|
---|
912 | {
|
---|
913 | PVBOXSERVICECTRLTHREAD pThread = NULL;
|
---|
914 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
915 | if (RT_SUCCESS(rc))
|
---|
916 | {
|
---|
917 | PVBOXSERVICECTRLTHREAD pThreadCur;
|
---|
918 | RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
|
---|
919 | {
|
---|
920 | if (pThreadCur->uPID == uPID)
|
---|
921 | {
|
---|
922 | rc = RTCritSectEnter(&pThreadCur->CritSect);
|
---|
923 | if (RT_SUCCESS(rc))
|
---|
924 | pThread = pThreadCur;
|
---|
925 | break;
|
---|
926 | }
|
---|
927 | }
|
---|
928 |
|
---|
929 | int rc2 = RTCritSectLeave(&g_csControlThreads);
|
---|
930 | if (RT_SUCCESS(rc))
|
---|
931 | rc = rc2;
|
---|
932 | }
|
---|
933 |
|
---|
934 | return pThread;
|
---|
935 | }
|
---|
936 |
|
---|
937 |
|
---|
938 | /**
|
---|
939 | * Unlocks a previously locked guest process thread.
|
---|
940 | *
|
---|
941 | * @param pThread Thread to unlock.
|
---|
942 | */
|
---|
943 | void VBoxServiceControlUnlockThread(const PVBOXSERVICECTRLTHREAD pThread)
|
---|
944 | {
|
---|
945 | AssertPtr(pThread);
|
---|
946 |
|
---|
947 | int rc = RTCritSectLeave(&pThread->CritSect);
|
---|
948 | AssertRC(rc);
|
---|
949 | }
|
---|
950 |
|
---|
951 |
|
---|
952 | /**
|
---|
953 | * Assigns a valid PID to a guest control thread and also checks if there already was
|
---|
954 | * another (stale) guest process which was using that PID before and destroys it.
|
---|
955 | *
|
---|
956 | * @return IPRT status code.
|
---|
957 | * @param pThread Thread to assign PID to.
|
---|
958 | * @param uPID PID to assign to the specified guest control execution thread.
|
---|
959 | */
|
---|
960 | int VBoxServiceControlAssignPID(PVBOXSERVICECTRLTHREAD pThread, uint32_t uPID)
|
---|
961 | {
|
---|
962 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
963 | AssertReturn(uPID, VERR_INVALID_PARAMETER);
|
---|
964 |
|
---|
965 | int rc = RTCritSectEnter(&g_csControlThreads);
|
---|
966 | if (RT_SUCCESS(rc))
|
---|
967 | {
|
---|
968 | /* Search old threads using the desired PID and shut them down completely -- it's
|
---|
969 | * not used anymore. */
|
---|
970 | PVBOXSERVICECTRLTHREAD pThreadCur;
|
---|
971 | bool fTryAgain = false;
|
---|
972 | do
|
---|
973 | {
|
---|
974 | RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
|
---|
975 | {
|
---|
976 | if (pThreadCur->uPID == uPID)
|
---|
977 | {
|
---|
978 | Assert(pThreadCur != pThread); /* can't happen */
|
---|
979 | uint32_t uTriedPID = uPID;
|
---|
980 | uPID += 391939;
|
---|
981 | VBoxServiceVerbose(2, "PID %u was used before, trying again with %u ...\n",
|
---|
982 | uTriedPID, uPID);
|
---|
983 | fTryAgain = true;
|
---|
984 | break;
|
---|
985 | }
|
---|
986 | }
|
---|
987 | } while (fTryAgain);
|
---|
988 |
|
---|
989 | /* Assign PID to current thread. */
|
---|
990 | pThread->uPID = uPID;
|
---|
991 |
|
---|
992 | rc = RTCritSectLeave(&g_csControlThreads);
|
---|
993 | AssertRC(rc);
|
---|
994 | }
|
---|
995 |
|
---|
996 | return rc;
|
---|
997 | }
|
---|
998 |
|
---|
999 |
|
---|
1000 | /**
|
---|
1001 | * The 'vminfo' service description.
|
---|
1002 | */
|
---|
1003 | VBOXSERVICE g_Control =
|
---|
1004 | {
|
---|
1005 | /* pszName. */
|
---|
1006 | "control",
|
---|
1007 | /* pszDescription. */
|
---|
1008 | "Host-driven Guest Control",
|
---|
1009 | /* pszUsage. */
|
---|
1010 | #ifdef DEBUG
|
---|
1011 | " [--control-dump-stderr] [--control-dump-stdout]\n"
|
---|
1012 | #endif
|
---|
1013 | " [--control-interval <ms>] [--control-procs-max-kept <x>]\n"
|
---|
1014 | " [--control-procs-mem-std[in|out|err] <KB>]"
|
---|
1015 | ,
|
---|
1016 | /* pszOptions. */
|
---|
1017 | #ifdef DEBUG
|
---|
1018 | " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
|
---|
1019 | " temporary directory.\n"
|
---|
1020 | " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
|
---|
1021 | " temporary directory.\n"
|
---|
1022 | #endif
|
---|
1023 | " --control-interval Specifies the interval at which to check for\n"
|
---|
1024 | " new control commands. The default is 1000 ms.\n"
|
---|
1025 | " --control-procs-max-kept\n"
|
---|
1026 | " Specifies how many started guest processes are\n"
|
---|
1027 | " kept into memory to work with. Default is 25.\n"
|
---|
1028 | ,
|
---|
1029 | /* methods */
|
---|
1030 | VBoxServiceControlPreInit,
|
---|
1031 | VBoxServiceControlOption,
|
---|
1032 | VBoxServiceControlInit,
|
---|
1033 | VBoxServiceControlWorker,
|
---|
1034 | VBoxServiceControlStop,
|
---|
1035 | VBoxServiceControlTerm
|
---|
1036 | };
|
---|
1037 |
|
---|