VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControl.cpp@ 43363

最後變更 在這個檔案從43363是 43259,由 vboxsync 提交於 13 年 前

fixed a few gcc false positive warnings and two shadowed declarations

  • 屬性 svn:eol-style 設為 native
  • 屬性 svn:keywords 設為 Author Date Id Revision
檔案大小: 45.4 KB
 
1/* $Id: VBoxServiceControl.cpp 43259 2012-09-09 16:02:15Z 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.alldomusa.eu.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
35using namespace guestControl;
36
37/*******************************************************************************
38* Global Variables *
39*******************************************************************************/
40/** The control interval (milliseconds). */
41static uint32_t g_uControlIntervalMS = 0;
42/** The semaphore we're blocking our main control thread on. */
43static RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
44/** The guest control service client ID. */
45static 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. */
49static 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). */
55static RTLISTANCHOR g_lstControlThreadsActive;
56/** List of inactive guest control threads (VBOXSERVICECTRLTHREAD). */
57static RTLISTANCHOR g_lstControlThreadsInactive;
58/** Critical section protecting g_GuestControlExecThreads. */
59static RTCRITSECT g_csControlThreads;
60/** List of guest control files (VBOXSERVICECTRLFILE).
61 **@todo Use a map (later). */
62static RTLISTANCHOR g_lstControlFiles;
63/** The internal file count for building our internal file handles.
64 * Should be enough for now. */
65static uint32_t g_uControlFileCount = 0;
66
67
68/*******************************************************************************
69* Internal Functions *
70*******************************************************************************/
71/** @todo Shorten "VBoxServiceControl" to "gstsvcCntl". */
72static int VBoxServiceControlReapThreads(void);
73static int VBoxServiceControlStartAllowed(bool *pbAllowed);
74static int VBoxServiceControlHandleCmdStartProc(uint32_t u32ClientId, uint32_t uNumParms);
75static int VBoxServiceControlHandleCmdSetInput(uint32_t u32ClientId, uint32_t uNumParms, void *pvScratchBuf, size_t cbScratchBuf);
76static int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms);
77static int VBoxServiceControlHandleFileOpen(uint32_t idClient, uint32_t cParms);
78static int VBoxServiceControlHandleFileClose(uint32_t idClient, uint32_t cParms);
79static int VBoxServiceControlHandleFileRead(uint32_t idClient, uint32_t cParms);
80static int VBoxServiceControlHandleFileWrite(uint32_t idClient, uint32_t cParms, void *pvScratchBuf, size_t cbScratchBuf);
81static int VBoxServiceControlHandleFileSeek(uint32_t idClient, uint32_t cParms);
82static int VBoxServiceControlHandleFileTell(uint32_t idClient, uint32_t cParms);
83
84#ifdef DEBUG
85static 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 */
118static 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 */
156static 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 */
184static 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 */
229DECLCALLBACK(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 */
346static 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 uTimeout Timeout (in ms) to wait for output becoming available.
443 * @param pvBuf Pointer to a pre-allocated buffer to store the output.
444 * @param cbBuf Size (in bytes) of the pre-allocated buffer.
445 * @param pcbRead Pointer to number of bytes read. Optional.
446 */
447int VBoxServiceControlExecGetOutput(uint32_t uPID, uint32_t uCID,
448 uint32_t uHandleId, uint32_t uTimeout,
449 void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
450{
451 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
452 AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
453 /* pcbRead is optional. */
454
455 int rc = VINF_SUCCESS;
456 VBOXSERVICECTRLREQUESTTYPE reqType;
457 switch (uHandleId)
458 {
459 case OUTPUT_HANDLE_ID_STDERR:
460 reqType = VBOXSERVICECTRLREQUEST_STDERR_READ;
461 break;
462
463 case OUTPUT_HANDLE_ID_STDOUT:
464 case OUTPUT_HANDLE_ID_STDOUT_DEPRECATED:
465 reqType = VBOXSERVICECTRLREQUEST_STDOUT_READ;
466 break;
467
468 default:
469 rc = VERR_INVALID_PARAMETER;
470 break;
471 }
472
473 PVBOXSERVICECTRLREQUEST pRequest;
474 if (RT_SUCCESS(rc))
475 {
476 rc = VBoxServiceControlThreadRequestAllocEx(&pRequest, reqType,
477 pvBuf, cbBuf, uCID);
478 if (RT_SUCCESS(rc))
479 rc = VBoxServiceControlThreadPerform(uPID, pRequest);
480
481 if (RT_SUCCESS(rc))
482 {
483 if (pcbRead)
484 *pcbRead = pRequest->cbData;
485 }
486
487 VBoxServiceControlThreadRequestFree(pRequest);
488 }
489
490 return rc;
491}
492
493
494/**
495 * Sets the specified guest thread to a certain list.
496 *
497 * @return IPRT status code.
498 * @param enmList List to move thread to.
499 * @param pThread Thread to set inactive.
500 */
501int VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLISTTYPE enmList,
502 PVBOXSERVICECTRLTHREAD pThread)
503{
504 AssertReturn(enmList > VBOXSERVICECTRLTHREADLIST_UNKNOWN, VERR_INVALID_PARAMETER);
505 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
506
507 int rc = RTCritSectEnter(&g_csControlThreads);
508 if (RT_SUCCESS(rc))
509 {
510 VBoxServiceVerbose(3, "Setting thread (PID %u) to list %d\n",
511 pThread->uPID, enmList);
512
513 PRTLISTANCHOR pAnchor = NULL;
514 switch (enmList)
515 {
516 case VBOXSERVICECTRLTHREADLIST_STOPPED:
517 pAnchor = &g_lstControlThreadsInactive;
518 break;
519
520 case VBOXSERVICECTRLTHREADLIST_RUNNING:
521 pAnchor = &g_lstControlThreadsActive;
522 break;
523
524 default:
525 AssertMsgFailed(("Unknown list type: %u", enmList));
526 break;
527 }
528
529 if (!pAnchor)
530 rc = VERR_INVALID_PARAMETER;
531
532 if (RT_SUCCESS(rc))
533 {
534 if (pThread->pAnchor != NULL)
535 {
536 /* If thread was assigned to a list before,
537 * remove the thread from the old list first. */
538 /* rc = */ RTListNodeRemove(&pThread->Node);
539 }
540
541 /* Add thread to desired list. */
542 /* rc = */ RTListAppend(pAnchor, &pThread->Node);
543 pThread->pAnchor = pAnchor;
544 }
545
546 int rc2 = RTCritSectLeave(&g_csControlThreads);
547 if (RT_SUCCESS(rc))
548 rc = rc2;
549 }
550
551 return VINF_SUCCESS;
552}
553
554
555/**
556 * Injects input to a specified running process.
557 *
558 * @return IPRT status code.
559 * @param uPID PID of process to set the input for.
560 * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
561 * @param pvBuf Pointer to a buffer containing the actual input data.
562 * @param cbBuf Size (in bytes) of the input buffer data.
563 * @param pcbWritten Pointer to number of bytes written to the process. Optional.
564 */
565int VBoxServiceControlSetInput(uint32_t uPID, uint32_t uCID,
566 bool fPendingClose,
567 void *pvBuf, uint32_t cbBuf,
568 uint32_t *pcbWritten)
569{
570 /* pvBuf is optional. */
571 /* cbBuf is optional. */
572 /* pcbWritten is optional. */
573
574 PVBOXSERVICECTRLREQUEST pRequest;
575 int rc = VBoxServiceControlThreadRequestAllocEx(&pRequest,
576 fPendingClose
577 ? VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF
578 : VBOXSERVICECTRLREQUEST_STDIN_WRITE,
579 pvBuf, cbBuf, uCID);
580 if (RT_SUCCESS(rc))
581 {
582 rc = VBoxServiceControlThreadPerform(uPID, pRequest);
583 if (RT_SUCCESS(rc))
584 {
585 if (pcbWritten)
586 *pcbWritten = pRequest->cbData;
587 }
588
589 VBoxServiceControlThreadRequestFree(pRequest);
590 }
591
592 return rc;
593}
594
595
596/**
597 * Handles input for a started process by copying the received data into its
598 * stdin pipe.
599 *
600 * @returns IPRT status code.
601 * @param idClient The HGCM client session ID.
602 * @param cParms The number of parameters the host is
603 * offering.
604 * @param pvScratchBuf The scratch buffer.
605 * @param cbScratchBuf The scratch buffer size for retrieving the input data.
606 */
607static int VBoxServiceControlHandleCmdSetInput(uint32_t idClient, uint32_t cParms,
608 void *pvScratchBuf, size_t cbScratchBuf)
609{
610 AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
611 AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
612
613 uint32_t uContextID;
614 uint32_t uPID;
615 uint32_t uFlags;
616 uint32_t cbSize;
617
618 uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
619 uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
620
621 /*
622 * Ask the host for the input data.
623 */
624 int rc = VbglR3GuestCtrlExecGetHostCmdInput(idClient, cParms,
625 &uContextID, &uPID, &uFlags,
626 pvScratchBuf, cbScratchBuf, &cbSize);
627 if (RT_FAILURE(rc))
628 {
629 VBoxServiceError("[PID %u]: Failed to retrieve exec input command! Error: %Rrc\n",
630 uPID, rc);
631 }
632 else if (cbSize > cbScratchBuf)
633 {
634 VBoxServiceError("[PID %u]: Too much input received! cbSize=%u, cbScratchBuf=%u\n",
635 uPID, cbSize, cbScratchBuf);
636 rc = VERR_INVALID_PARAMETER;
637 }
638 else
639 {
640 /*
641 * Is this the last input block we need to deliver? Then let the pipe know ...
642 */
643 bool fPendingClose = false;
644 if (uFlags & INPUT_FLAG_EOF)
645 {
646 fPendingClose = true;
647 VBoxServiceVerbose(4, "[PID %u]: Got last input block of size %u ...\n",
648 uPID, cbSize);
649 }
650
651 rc = VBoxServiceControlSetInput(uPID, uContextID, fPendingClose, pvScratchBuf,
652 cbSize, &cbWritten);
653 VBoxServiceVerbose(4, "[PID %u]: Written input, CID=%u, rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
654 uPID, uContextID, rc, uFlags, fPendingClose, cbSize, cbWritten);
655 if (RT_SUCCESS(rc))
656 {
657 uStatus = INPUT_STS_WRITTEN;
658 uFlags = 0; /* No flags at the moment. */
659 }
660 else
661 {
662 if (rc == VERR_BAD_PIPE)
663 uStatus = INPUT_STS_TERMINATED;
664 else if (rc == VERR_BUFFER_OVERFLOW)
665 uStatus = INPUT_STS_OVERFLOW;
666 }
667 }
668
669 /*
670 * If there was an error and we did not set the host status
671 * yet, then do it now.
672 */
673 if ( RT_FAILURE(rc)
674 && uStatus == INPUT_STS_UNDEFINED)
675 {
676 uStatus = INPUT_STS_ERROR;
677 uFlags = rc;
678 }
679 Assert(uStatus > INPUT_STS_UNDEFINED);
680
681 VBoxServiceVerbose(3, "[PID %u]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
682 uPID, uContextID, uStatus, uFlags, cbWritten);
683
684 /* Note: Since the context ID is unique the request *has* to be completed here,
685 * regardless whether we got data or not! Otherwise the progress object
686 * on the host never will get completed! */
687 rc = VbglR3GuestCtrlExecReportStatusIn(idClient, uContextID, uPID,
688 uStatus, uFlags, (uint32_t)cbWritten);
689
690 if (RT_FAILURE(rc))
691 VBoxServiceError("[PID %u]: Failed to report input status! Error: %Rrc\n",
692 uPID, rc);
693 return rc;
694}
695
696
697static PVBOXSERVICECTRLFILE VBoxControlGetFile(uint32_t uHandle)
698{
699 PVBOXSERVICECTRLFILE pFileCur = NULL;
700 /** @todo Use a map later! */
701 RTListForEach(&g_lstControlFiles, pFileCur, VBOXSERVICECTRLFILE, Node)
702 {
703 if (pFileCur->uHandle == uHandle)
704 return pFileCur;
705 }
706
707 return NULL;
708}
709
710
711static int VBoxServiceControlHandleFileOpen(uint32_t idClient, uint32_t cParms)
712{
713 uint32_t uContextID;
714
715 char szFile[RTPATH_MAX];
716 char szOpenMode[64];
717 char szDisposition[64];
718 uint32_t uCreationMode;
719 uint64_t uOffset;
720
721 int rc = VbglR3GuestCtrlFileGetHostCmdOpen(idClient, cParms, &uContextID,
722 /* File to open. */
723 szFile, sizeof(szFile),
724 /* Open mode. */
725 szOpenMode, sizeof(szOpenMode),
726 /* Disposition. */
727 szDisposition, sizeof(szDisposition),
728 /* Creation mode. */
729 &uCreationMode,
730 /* Offset. */
731 &uOffset);
732 if (RT_SUCCESS(rc))
733 {
734 PVBOXSERVICECTRLFILE pFile = (PVBOXSERVICECTRLFILE)RTMemAlloc(sizeof(VBOXSERVICECTRLFILE));
735 if (!pFile)
736 return VERR_NO_MEMORY;
737
738 if (!RTStrPrintf(pFile->szName, sizeof(pFile->szName), "%s", szFile))
739 rc = VERR_BUFFER_UNDERFLOW;
740
741 if (RT_SUCCESS(rc))
742 {
743 uint64_t fFlags = RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE; /** @todo Modes! */
744 rc = RTFileOpen(&pFile->hFile, pFile->szName, fFlags);
745 if ( RT_SUCCESS(rc)
746 && uOffset)
747 {
748 /* Seeking is optional. */
749 int rc2 = RTFileSeek(pFile->hFile, (int64_t)uOffset, RTFILE_SEEK_BEGIN, NULL /* Current offset */);
750 if (RT_FAILURE(rc2))
751 VBoxServiceVerbose(3, "[File %s]: Seeking to offset %RU64 failed; rc=%Rrc\n",
752 pFile->szName, uOffset, rc);
753 }
754 else
755 VBoxServiceVerbose(3, "[File %s]: Opening failed; rc=%Rrc\n",
756 pFile->szName, rc);
757 }
758
759 uint32_t uHandle = 0;
760 if (RT_SUCCESS(rc))
761 {
762 VBoxServiceVerbose(3, "[File %s]: Opened.\n", pFile->szName);
763
764 uHandle = g_uControlFileCount++;
765 pFile->uHandle = uHandle;
766 /* rc = */ RTListAppend(&g_lstControlFiles, &pFile->Node);
767 }
768
769 if (RT_FAILURE(rc))
770 RTMemFree(pFile);
771
772 /* Report back in any case. */
773 int rc2 = VbglR3GuestCtrlFileNotify(idClient, uContextID, uHandle,
774 GUESTFILENOTIFYTYPE_OPEN, &rc, sizeof(rc));
775 if (RT_FAILURE(rc2))
776 VBoxServiceError("[File %s]: Failed to report open status, rc=%Rrc\n",
777 szFile, rc2);
778 if (RT_SUCCESS(rc))
779 rc = rc2;
780 }
781 return rc;
782}
783
784
785static int VBoxServiceControlHandleFileClose(uint32_t idClient, uint32_t cParms)
786{
787 uint32_t uContextID;
788 uint32_t uHandle;
789
790 int rc = VbglR3GuestCtrlFileGetHostCmdClose(idClient, cParms, &uContextID,
791 /* File handle to close. */
792 &uHandle);
793 if (RT_SUCCESS(rc))
794 {
795 PVBOXSERVICECTRLFILE pFile = VBoxControlGetFile(uHandle);
796 if (pFile)
797 {
798 rc = RTFileClose(pFile->hFile);
799 }
800 else
801 rc = VERR_NOT_FOUND;
802
803 /* Report back in any case. */
804 int rc2 = VbglR3GuestCtrlFileNotify(idClient, uContextID, uHandle,
805 GUESTFILENOTIFYTYPE_CLOSE, &rc, sizeof(rc));
806 if (RT_FAILURE(rc2))
807 VBoxServiceError("Failed to report close status, rc=%Rrc\n", rc2);
808 if (RT_SUCCESS(rc))
809 rc = rc2;
810 }
811 return rc;
812}
813
814
815static int VBoxServiceControlHandleFileRead(uint32_t idClient, uint32_t cParms)
816{
817 uint32_t uContextID;
818 uint32_t uHandle;
819 uint32_t cbToRead;
820
821 int rc = VbglR3GuestCtrlFileGetHostCmdRead(idClient, cParms, &uContextID,
822 &uHandle, &cbToRead);
823 if (RT_SUCCESS(rc))
824 {
825
826 }
827 return rc;
828}
829
830
831static int VBoxServiceControlHandleFileWrite(uint32_t idClient, uint32_t cParms,
832 void *pvScratchBuf, size_t cbScratchBuf)
833{
834 AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
835 AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
836
837 uint32_t uContextID;
838 uint32_t uHandle;
839 uint32_t cbToWrite;
840
841 int rc = VbglR3GuestCtrlFileGetHostCmdWrite(idClient, cParms, &uContextID,
842 &uHandle, pvScratchBuf, cbScratchBuf,
843 &cbToWrite);
844 if (RT_SUCCESS(rc))
845 {
846
847 }
848 return rc;
849}
850
851
852static int VBoxServiceControlHandleFileSeek(uint32_t idClient, uint32_t cParms)
853{
854 uint32_t uContextID;
855 uint32_t uHandle;
856 uint32_t uSeekMethod;
857 uint64_t uOffset; /* Will be converted to int64_t. */
858
859 int rc = VbglR3GuestCtrlFileGetHostCmdSeek(idClient, cParms, &uContextID,
860 &uHandle, &uSeekMethod, &uOffset);
861 if (RT_SUCCESS(rc))
862 {
863
864 }
865 return rc;
866}
867
868
869static int VBoxServiceControlHandleFileTell(uint32_t idClient, uint32_t cParms)
870{
871 uint32_t uContextID;
872 uint32_t uHandle;
873
874 int rc = VbglR3GuestCtrlFileGetHostCmdTell(idClient, cParms, &uContextID,
875 &uHandle);
876 if (RT_SUCCESS(rc))
877 {
878
879 }
880 return rc;
881}
882
883
884/**
885 * Handles the guest control output command.
886 *
887 * @return IPRT status code.
888 * @param idClient The HGCM client session ID.
889 * @param cParms The number of parameters the host is offering.
890 */
891static int VBoxServiceControlHandleCmdGetOutput(uint32_t idClient, uint32_t cParms)
892{
893 uint32_t uContextID;
894 uint32_t uPID;
895 uint32_t uHandleID;
896 uint32_t uFlags;
897
898 int rc = VbglR3GuestCtrlExecGetHostCmdOutput(idClient, cParms,
899 &uContextID, &uPID, &uHandleID, &uFlags);
900 if (RT_SUCCESS(rc))
901 {
902 uint8_t *pBuf = (uint8_t*)RTMemAlloc(_64K);
903 if (pBuf)
904 {
905 uint32_t cbRead = 0;
906 rc = VBoxServiceControlExecGetOutput(uPID, uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
907 pBuf, _64K /* cbSize */, &cbRead);
908 VBoxServiceVerbose(3, "[PID %u]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
909 uPID, rc, uContextID, cbRead, uHandleID, uFlags);
910
911#ifdef DEBUG
912 if ( g_fControlDumpStdErr
913 && uHandleID == OUTPUT_HANDLE_ID_STDERR)
914 {
915 char szPID[RTPATH_MAX];
916 if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
917 rc = VERR_BUFFER_UNDERFLOW;
918 if (RT_SUCCESS(rc))
919 rc = vboxServiceControlDump(szPID, pBuf, cbRead);
920 }
921 else if ( g_fControlDumpStdOut
922 && ( uHandleID == OUTPUT_HANDLE_ID_STDOUT
923 || uHandleID == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED))
924 {
925 char szPID[RTPATH_MAX];
926 if (!RTStrPrintf(szPID, sizeof(szPID), "VBoxService_PID%u_StdOut.txt", uPID))
927 rc = VERR_BUFFER_UNDERFLOW;
928 if (RT_SUCCESS(rc))
929 rc = vboxServiceControlDump(szPID, pBuf, cbRead);
930 AssertRC(rc);
931 }
932#endif
933 /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
934 * data which the host needs to work with -- so just pass through all data unfiltered! */
935
936 /* Note: Since the context ID is unique the request *has* to be completed here,
937 * regardless whether we got data or not! Otherwise the progress object
938 * on the host never will get completed! */
939 int rc2 = VbglR3GuestCtrlExecSendOut(idClient, uContextID, uPID, uHandleID, uFlags,
940 pBuf, cbRead);
941 if (RT_SUCCESS(rc))
942 rc = rc2;
943 else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
944 rc = VINF_SUCCESS;
945
946 RTMemFree(pBuf);
947 }
948 else
949 rc = VERR_NO_MEMORY;
950 }
951
952 if (RT_FAILURE(rc))
953 VBoxServiceError("[PID %u]: Error handling output command! Error: %Rrc\n",
954 uPID, rc);
955 return rc;
956}
957
958
959/** @copydoc VBOXSERVICE::pfnStop */
960static DECLCALLBACK(void) VBoxServiceControlStop(void)
961{
962 VBoxServiceVerbose(3, "Stopping ...\n");
963
964 /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
965 * annoying call since doesn't support timeouts in the posix world. */
966 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
967 RTSemEventMultiSignal(g_hControlEvent);
968
969 /*
970 * Ask the host service to cancel all pending requests so that we can
971 * shutdown properly here.
972 */
973 if (g_uControlSvcClientID)
974 {
975 VBoxServiceVerbose(3, "Cancelling pending waits (client ID=%u) ...\n",
976 g_uControlSvcClientID);
977
978 int rc = VbglR3GuestCtrlCancelPendingWaits(g_uControlSvcClientID);
979 if (RT_FAILURE(rc))
980 VBoxServiceError("Cancelling pending waits failed; rc=%Rrc\n", rc);
981 }
982}
983
984
985/**
986 * Reaps all inactive guest process threads.
987 *
988 * @return IPRT status code.
989 */
990static int VBoxServiceControlReapThreads(void)
991{
992 int rc = RTCritSectEnter(&g_csControlThreads);
993 if (RT_SUCCESS(rc))
994 {
995 PVBOXSERVICECTRLTHREAD pThread =
996 RTListGetFirst(&g_lstControlThreadsInactive, VBOXSERVICECTRLTHREAD, Node);
997 while (pThread)
998 {
999 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
1000 bool fLast = RTListNodeIsLast(&g_lstControlThreadsInactive, &pThread->Node);
1001 int rc2 = VBoxServiceControlThreadWait(pThread, 30 * 1000 /* 30 seconds max. */,
1002 NULL /* rc */);
1003 if (RT_SUCCESS(rc2))
1004 {
1005 RTListNodeRemove(&pThread->Node);
1006
1007 rc2 = VBoxServiceControlThreadFree(pThread);
1008 if (RT_FAILURE(rc2))
1009 {
1010 VBoxServiceError("Freeing guest process thread failed with rc=%Rrc\n", rc2);
1011 if (RT_SUCCESS(rc)) /* Keep original failure. */
1012 rc = rc2;
1013 }
1014 }
1015 else
1016 VBoxServiceError("Waiting on guest process thread failed with rc=%Rrc\n", rc2);
1017 /* Keep going. */
1018
1019 if (fLast)
1020 break;
1021
1022 pThread = pNext;
1023 }
1024
1025 int rc2 = RTCritSectLeave(&g_csControlThreads);
1026 if (RT_SUCCESS(rc))
1027 rc = rc2;
1028 }
1029
1030 VBoxServiceVerbose(4, "Reaping threads returned with rc=%Rrc\n", rc);
1031 return rc;
1032}
1033
1034
1035/**
1036 * Destroys all guest process threads which are still active.
1037 */
1038static void VBoxServiceControlShutdown(void)
1039{
1040 VBoxServiceVerbose(2, "Shutting down ...\n");
1041
1042 /* Signal all threads in the active list that we want to shutdown. */
1043 PVBOXSERVICECTRLTHREAD pThread;
1044 RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
1045 VBoxServiceControlThreadStop(pThread);
1046
1047 /* Wait for all active threads to shutdown and destroy the active thread list. */
1048 pThread = RTListGetFirst(&g_lstControlThreadsActive, VBOXSERVICECTRLTHREAD, Node);
1049 while (pThread)
1050 {
1051 PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
1052 bool fLast = RTListNodeIsLast(&g_lstControlThreadsActive, &pThread->Node);
1053
1054 int rc2 = VBoxServiceControlThreadWait(pThread,
1055 30 * 1000 /* Wait 30 seconds max. */,
1056 NULL /* rc */);
1057 if (RT_FAILURE(rc2))
1058 VBoxServiceError("Guest process thread failed to stop; rc=%Rrc\n", rc2);
1059
1060 if (fLast)
1061 break;
1062
1063 pThread = pNext;
1064 }
1065
1066 int rc2 = VBoxServiceControlReapThreads();
1067 if (RT_FAILURE(rc2))
1068 VBoxServiceError("Reaping inactive threads failed with rc=%Rrc\n", rc2);
1069
1070 AssertMsg(RTListIsEmpty(&g_lstControlThreadsActive),
1071 ("Guest process active thread list still contains entries when it should not\n"));
1072 AssertMsg(RTListIsEmpty(&g_lstControlThreadsInactive),
1073 ("Guest process inactive thread list still contains entries when it should not\n"));
1074
1075 /* Destroy critical section. */
1076 RTCritSectDelete(&g_csControlThreads);
1077
1078 /* Close all left guest files. */
1079 PVBOXSERVICECTRLFILE pFile;
1080 pFile = RTListGetFirst(&g_lstControlFiles, VBOXSERVICECTRLFILE, Node);
1081 while (pFile)
1082 {
1083 PVBOXSERVICECTRLFILE pNext = RTListNodeGetNext(&pFile->Node, VBOXSERVICECTRLFILE, Node);
1084 bool fLast = RTListNodeIsLast(&g_lstControlFiles, &pFile->Node);
1085
1086 rc2 = RTFileClose(pFile->hFile);
1087 if (RT_FAILURE(rc2))
1088 {
1089 VBoxServiceError("Unable to close file \"%s\"; rc=%Rrc\n",
1090 pFile->szName, rc2);
1091 /* Keep going. */
1092 }
1093
1094 RTListNodeRemove(&pFile->Node);
1095
1096 if (fLast)
1097 break;
1098
1099 pFile = pNext;
1100 }
1101
1102 AssertMsg(RTListIsEmpty(&g_lstControlFiles),
1103 ("Guest file list still contains entries when it should not\n"));
1104
1105 VBoxServiceVerbose(2, "Shutting down complete\n");
1106}
1107
1108
1109/** @copydoc VBOXSERVICE::pfnTerm */
1110static DECLCALLBACK(void) VBoxServiceControlTerm(void)
1111{
1112 VBoxServiceVerbose(3, "Terminating ...\n");
1113
1114 VBoxServiceControlShutdown();
1115
1116 VBoxServiceVerbose(3, "Disconnecting client ID=%u ...\n",
1117 g_uControlSvcClientID);
1118 VbglR3GuestCtrlDisconnect(g_uControlSvcClientID);
1119 g_uControlSvcClientID = 0;
1120
1121 if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
1122 {
1123 RTSemEventMultiDestroy(g_hControlEvent);
1124 g_hControlEvent = NIL_RTSEMEVENTMULTI;
1125 }
1126}
1127
1128
1129/**
1130 * Determines whether starting a new guest process according to the
1131 * maximum number of concurrent guest processes defined is allowed or not.
1132 *
1133 * @return IPRT status code.
1134 * @param pbAllowed True if starting (another) guest process
1135 * is allowed, false if not.
1136 */
1137static int VBoxServiceControlStartAllowed(bool *pbAllowed)
1138{
1139 AssertPtrReturn(pbAllowed, VERR_INVALID_POINTER);
1140
1141 int rc = RTCritSectEnter(&g_csControlThreads);
1142 if (RT_SUCCESS(rc))
1143 {
1144 /*
1145 * Check if we're respecting our memory policy by checking
1146 * how many guest processes are started and served already.
1147 */
1148 bool fLimitReached = false;
1149 if (g_uControlProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
1150 {
1151 uint32_t uProcsRunning = 0;
1152 PVBOXSERVICECTRLTHREAD pThread;
1153 RTListForEach(&g_lstControlThreadsActive, pThread, VBOXSERVICECTRLTHREAD, Node)
1154 uProcsRunning++;
1155
1156 VBoxServiceVerbose(3, "Maximum served guest processes set to %u, running=%u\n",
1157 g_uControlProcsMaxKept, uProcsRunning);
1158
1159 int32_t iProcsLeft = (g_uControlProcsMaxKept - uProcsRunning - 1);
1160 if (iProcsLeft < 0)
1161 {
1162 VBoxServiceVerbose(3, "Maximum running guest processes reached (%u)\n",
1163 g_uControlProcsMaxKept);
1164 fLimitReached = true;
1165 }
1166 }
1167
1168 *pbAllowed = !fLimitReached;
1169
1170 int rc2 = RTCritSectLeave(&g_csControlThreads);
1171 if (RT_SUCCESS(rc))
1172 rc = rc2;
1173 }
1174
1175 return rc;
1176}
1177
1178
1179/**
1180 * Finds a (formerly) started process given by its PID and locks it. Must be unlocked
1181 * by the caller with VBoxServiceControlThreadUnlock().
1182 *
1183 * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
1184 * @param uPID PID to search for.
1185 */
1186PVBOXSERVICECTRLTHREAD VBoxServiceControlLockThread(uint32_t uPID)
1187{
1188 PVBOXSERVICECTRLTHREAD pThread = NULL;
1189 int rc = RTCritSectEnter(&g_csControlThreads);
1190 if (RT_SUCCESS(rc))
1191 {
1192 PVBOXSERVICECTRLTHREAD pThreadCur;
1193 RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
1194 {
1195 if (pThreadCur->uPID == uPID)
1196 {
1197 rc = RTCritSectEnter(&pThreadCur->CritSect);
1198 if (RT_SUCCESS(rc))
1199 pThread = pThreadCur;
1200 break;
1201 }
1202 }
1203
1204 int rc2 = RTCritSectLeave(&g_csControlThreads);
1205 if (RT_SUCCESS(rc))
1206 rc = rc2;
1207 }
1208
1209 return pThread;
1210}
1211
1212
1213/**
1214 * Unlocks a previously locked guest process thread.
1215 *
1216 * @param pThread Thread to unlock.
1217 */
1218void VBoxServiceControlUnlockThread(const PVBOXSERVICECTRLTHREAD pThread)
1219{
1220 AssertPtr(pThread);
1221
1222 int rc = RTCritSectLeave(&pThread->CritSect);
1223 AssertRC(rc);
1224}
1225
1226
1227/**
1228 * Assigns a valid PID to a guest control thread and also checks if there already was
1229 * another (stale) guest process which was using that PID before and destroys it.
1230 *
1231 * @return IPRT status code.
1232 * @param pThread Thread to assign PID to.
1233 * @param uPID PID to assign to the specified guest control execution thread.
1234 */
1235int VBoxServiceControlAssignPID(PVBOXSERVICECTRLTHREAD pThread, uint32_t uPID)
1236{
1237 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
1238 AssertReturn(uPID, VERR_INVALID_PARAMETER);
1239
1240 int rc = RTCritSectEnter(&g_csControlThreads);
1241 if (RT_SUCCESS(rc))
1242 {
1243 /* Search old threads using the desired PID and shut them down completely -- it's
1244 * not used anymore. */
1245 PVBOXSERVICECTRLTHREAD pThreadCur;
1246 bool fTryAgain = false;
1247 do
1248 {
1249 RTListForEach(&g_lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
1250 {
1251 if (pThreadCur->uPID == uPID)
1252 {
1253 Assert(pThreadCur != pThread); /* can't happen */
1254 uint32_t uTriedPID = uPID;
1255 uPID += 391939;
1256 VBoxServiceVerbose(2, "PID %u was used before, trying again with %u ...\n",
1257 uTriedPID, uPID);
1258 fTryAgain = true;
1259 break;
1260 }
1261 }
1262 } while (fTryAgain);
1263
1264 /* Assign PID to current thread. */
1265 pThread->uPID = uPID;
1266
1267 rc = RTCritSectLeave(&g_csControlThreads);
1268 AssertRC(rc);
1269 }
1270
1271 return rc;
1272}
1273
1274
1275/**
1276 * The 'vminfo' service description.
1277 */
1278VBOXSERVICE g_Control =
1279{
1280 /* pszName. */
1281 "control",
1282 /* pszDescription. */
1283 "Host-driven Guest Control",
1284 /* pszUsage. */
1285#ifdef DEBUG
1286 " [--control-dump-stderr] [--control-dump-stdout]\n"
1287#endif
1288 " [--control-interval <ms>] [--control-procs-max-kept <x>]\n"
1289 " [--control-procs-mem-std[in|out|err] <KB>]"
1290 ,
1291 /* pszOptions. */
1292#ifdef DEBUG
1293 " --control-dump-stderr Dumps all guest proccesses stderr data to the\n"
1294 " temporary directory.\n"
1295 " --control-dump-stdout Dumps all guest proccesses stdout data to the\n"
1296 " temporary directory.\n"
1297#endif
1298 " --control-interval Specifies the interval at which to check for\n"
1299 " new control commands. The default is 1000 ms.\n"
1300 " --control-procs-max-kept\n"
1301 " Specifies how many started guest processes are\n"
1302 " kept into memory to work with. Default is 256.\n"
1303 ,
1304 /* methods */
1305 VBoxServiceControlPreInit,
1306 VBoxServiceControlOption,
1307 VBoxServiceControlInit,
1308 VBoxServiceControlWorker,
1309 VBoxServiceControlStop,
1310 VBoxServiceControlTerm
1311};
1312
注意: 瀏覽 TracBrowser 來幫助您使用儲存庫瀏覽器

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette