VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlThread.cpp@ 43898

Last change on this file since 43898 was 43196, checked in by vboxsync, 12 years ago

Fixed UINT32_MAX check.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 65.7 KB
Line 
1/* $Id: VBoxServiceControlThread.cpp 43196 2012-09-05 10:42:53Z vboxsync $ */
2/** @file
3 * VBoxServiceControlExecThread - Thread for every started guest process.
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/env.h>
25#include <iprt/file.h>
26#include <iprt/getopt.h>
27#include <iprt/handle.h>
28#include <iprt/mem.h>
29#include <iprt/path.h>
30#include <iprt/pipe.h>
31#include <iprt/poll.h>
32#include <iprt/process.h>
33#include <iprt/semaphore.h>
34#include <iprt/string.h>
35#include <iprt/thread.h>
36
37#include <VBox/VBoxGuestLib.h>
38#include <VBox/HostServices/GuestControlSvc.h>
39
40#include "VBoxServiceInternal.h"
41
42using namespace guestControl;
43
44/* Internal functions. */
45static int vboxServiceControlThreadRequestCancel(PVBOXSERVICECTRLREQUEST pThread);
46
47/**
48 * Initialies the passed in thread data structure with the parameters given.
49 *
50 * @return IPRT status code.
51 * @param pThread The thread's handle to allocate the data for.
52 * @param u32ContextID The context ID bound to this request / command.
53 @ @param pProcess Process information.
54 */
55static int gstsvcCntlExecThreadInit(PVBOXSERVICECTRLTHREAD pThread,
56 PVBOXSERVICECTRLPROCESS pProcess,
57 uint32_t u32ContextID)
58{
59 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
60 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
61
62 /* General stuff. */
63 pThread->pAnchor = NULL;
64 pThread->Node.pPrev = NULL;
65 pThread->Node.pNext = NULL;
66
67 pThread->fShutdown = false;
68 pThread->fStarted = false;
69 pThread->fStopped = false;
70
71 pThread->uContextID = u32ContextID;
72 /* ClientID will be assigned when thread is started; every guest
73 * process has its own client ID to detect crashes on a per-guest-process
74 * level. */
75
76 int rc = RTCritSectInit(&pThread->CritSect);
77 if (RT_FAILURE(rc))
78 return rc;
79
80 pThread->uPID = 0; /* Don't have a PID yet. */
81 pThread->pRequest = NULL; /* No request assigned yet. */
82 pThread->uFlags = pProcess->uFlags;
83 pThread->uTimeLimitMS = ( pProcess->uTimeLimitMS == UINT32_MAX
84 || pProcess->uTimeLimitMS == 0)
85 ? RT_INDEFINITE_WAIT : pProcess->uTimeLimitMS;
86
87 /* Prepare argument list. */
88 pThread->uNumArgs = 0; /* Initialize in case of RTGetOptArgvFromString() is failing ... */
89 rc = RTGetOptArgvFromString(&pThread->papszArgs, (int*)&pThread->uNumArgs,
90 (pProcess->uNumArgs > 0) ? pProcess->szArgs : "", NULL);
91 /* Did we get the same result? */
92 Assert(pProcess->uNumArgs == pThread->uNumArgs);
93
94 if (RT_SUCCESS(rc))
95 {
96 /* Prepare environment list. */
97 pThread->uNumEnvVars = 0;
98 if (pProcess->uNumEnvVars)
99 {
100 pThread->papszEnv = (char **)RTMemAlloc(pProcess->uNumEnvVars * sizeof(char*));
101 AssertPtr(pThread->papszEnv);
102 pThread->uNumEnvVars = pProcess->uNumEnvVars;
103
104 const char *pszCur = pProcess->szEnv;
105 uint32_t i = 0;
106 uint32_t cbLen = 0;
107 while (cbLen < pProcess->cbEnv)
108 {
109 /* sanity check */
110 if (i >= pProcess->uNumEnvVars)
111 {
112 rc = VERR_INVALID_PARAMETER;
113 break;
114 }
115 int cbStr = RTStrAPrintf(&pThread->papszEnv[i++], "%s", pszCur);
116 if (cbStr < 0)
117 {
118 rc = VERR_NO_STR_MEMORY;
119 break;
120 }
121 pszCur += cbStr + 1; /* Skip terminating '\0' */
122 cbLen += cbStr + 1; /* Skip terminating '\0' */
123 }
124 Assert(i == pThread->uNumEnvVars);
125 }
126
127 /* The actual command to execute. */
128 pThread->pszCmd = RTStrDup(pProcess->szCmd);
129 AssertPtr(pThread->pszCmd);
130
131 /* User management. */
132 pThread->pszUser = RTStrDup(pProcess->szUser);
133 AssertPtr(pThread->pszUser);
134 pThread->pszPassword = RTStrDup(pProcess->szPassword);
135 AssertPtr(pThread->pszPassword);
136 }
137
138 if (RT_FAILURE(rc)) /* Clean up on failure. */
139 VBoxServiceControlThreadFree(pThread);
140 return rc;
141}
142
143
144/**
145 * Frees a guest thread.
146 *
147 * @return IPRT status code.
148 * @param pThread Thread to shut down.
149 */
150int VBoxServiceControlThreadFree(PVBOXSERVICECTRLTHREAD pThread)
151{
152 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
153
154 VBoxServiceVerbose(3, "[PID %u]: Freeing ...\n",
155 pThread->uPID);
156
157 int rc = RTCritSectEnter(&pThread->CritSect);
158 if (RT_SUCCESS(rc))
159 {
160 if (pThread->uNumEnvVars)
161 {
162 for (uint32_t i = 0; i < pThread->uNumEnvVars; i++)
163 RTStrFree(pThread->papszEnv[i]);
164 RTMemFree(pThread->papszEnv);
165 }
166 RTGetOptArgvFree(pThread->papszArgs);
167
168 RTStrFree(pThread->pszCmd);
169 RTStrFree(pThread->pszUser);
170 RTStrFree(pThread->pszPassword);
171
172 VBoxServiceVerbose(3, "[PID %u]: Setting stopped state\n",
173 pThread->uPID);
174
175 rc = RTCritSectLeave(&pThread->CritSect);
176 AssertRC(rc);
177 }
178
179 /*
180 * Destroy other thread data.
181 */
182 if (RTCritSectIsInitialized(&pThread->CritSect))
183 RTCritSectDelete(&pThread->CritSect);
184
185 /*
186 * Destroy thread structure as final step.
187 */
188 RTMemFree(pThread);
189 pThread = NULL;
190
191 return rc;
192}
193
194
195/**
196 * Signals a guest process thread that we want it to shut down in
197 * a gentle way.
198 *
199 * @return IPRT status code.
200 * @param pThread Thread to shut down.
201 */
202int VBoxServiceControlThreadStop(const PVBOXSERVICECTRLTHREAD pThread)
203{
204 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
205
206 VBoxServiceVerbose(3, "[PID %u]: Stopping ...\n",
207 pThread->uPID);
208
209 int rc = vboxServiceControlThreadRequestCancel(pThread->pRequest);
210 if (RT_FAILURE(rc))
211 VBoxServiceError("[PID %u]: Signalling request event failed, rc=%Rrc\n",
212 pThread->uPID, rc);
213
214 /* Do *not* set pThread->fShutdown or other stuff here!
215 * The guest thread loop will do that as soon as it processes the quit message. */
216
217 PVBOXSERVICECTRLREQUEST pRequest;
218 rc = VBoxServiceControlThreadRequestAlloc(&pRequest, VBOXSERVICECTRLREQUEST_QUIT);
219 if (RT_SUCCESS(rc))
220 {
221 rc = VBoxServiceControlThreadPerform(pThread->uPID, pRequest);
222 if (RT_FAILURE(rc))
223 VBoxServiceVerbose(3, "[PID %u]: Sending quit request failed with rc=%Rrc\n",
224 pThread->uPID, rc);
225
226 VBoxServiceControlThreadRequestFree(pRequest);
227 }
228 return rc;
229}
230
231
232/**
233 * Wait for a guest process thread to shut down.
234 *
235 * @return IPRT status code.
236 * @param pThread Thread to wait shutting down for.
237 * @param RTMSINTERVAL Timeout in ms to wait for shutdown.
238 * @param prc Where to store the thread's return code. Optional.
239 */
240int VBoxServiceControlThreadWait(const PVBOXSERVICECTRLTHREAD pThread,
241 RTMSINTERVAL msTimeout, int *prc)
242{
243 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
244 /* prc is optional. */
245
246 int rc = VINF_SUCCESS;
247 if ( pThread->Thread != NIL_RTTHREAD
248 && ASMAtomicReadBool(&pThread->fStarted))
249 {
250 VBoxServiceVerbose(2, "[PID %u]: Waiting for shutdown of pThread=0x%p = \"%s\"...\n",
251 pThread->uPID, pThread, pThread->pszCmd);
252
253 /* Wait a bit ... */
254 int rcThread;
255 rc = RTThreadWait(pThread->Thread, msTimeout, &rcThread);
256 if (RT_FAILURE(rc))
257 {
258 VBoxServiceError("[PID %u]: Waiting for shutting down thread returned error rc=%Rrc\n",
259 pThread->uPID, rc);
260 }
261 else
262 {
263 VBoxServiceVerbose(3, "[PID %u]: Thread reported exit code=%Rrc\n",
264 pThread->uPID, rcThread);
265 if (prc)
266 *prc = rcThread;
267 }
268 }
269 return rc;
270}
271
272
273/**
274 * Closes the stdin pipe of a guest process.
275 *
276 * @return IPRT status code.
277 * @param hPollSet The polling set.
278 * @param phStdInW The standard input pipe handle.
279 */
280static int VBoxServiceControlThreadCloseStdIn(RTPOLLSET hPollSet, PRTPIPE phStdInW)
281{
282 AssertPtrReturn(phStdInW, VERR_INVALID_POINTER);
283
284 int rc = RTPollSetRemove(hPollSet, VBOXSERVICECTRLPIPEID_STDIN);
285 if (rc != VERR_POLL_HANDLE_ID_NOT_FOUND)
286 AssertRC(rc);
287
288 if (*phStdInW != NIL_RTPIPE)
289 {
290 rc = RTPipeClose(*phStdInW);
291 AssertRC(rc);
292 *phStdInW = NIL_RTPIPE;
293 }
294
295 return rc;
296}
297
298
299/**
300 * Handle an error event on standard input.
301 *
302 * @return IPRT status code.
303 * @param hPollSet The polling set.
304 * @param fPollEvt The event mask returned by RTPollNoResume.
305 * @param phStdInW The standard input pipe handle.
306 */
307static int VBoxServiceControlThreadHandleStdInErrorEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW)
308{
309 NOREF(fPollEvt);
310
311 return VBoxServiceControlThreadCloseStdIn(hPollSet, phStdInW);
312}
313
314
315/**
316 * Handle pending output data or error on standard out or standard error.
317 *
318 * @returns IPRT status code from client send.
319 * @param hPollSet The polling set.
320 * @param fPollEvt The event mask returned by RTPollNoResume.
321 * @param phPipeR The pipe handle.
322 * @param idPollHnd The pipe ID to handle.
323 *
324 */
325static int VBoxServiceControlThreadHandleOutputError(RTPOLLSET hPollSet, uint32_t fPollEvt,
326 PRTPIPE phPipeR, uint32_t idPollHnd)
327{
328 AssertPtrReturn(phPipeR, VERR_INVALID_POINTER);
329
330#ifdef DEBUG
331 VBoxServiceVerbose(4, "VBoxServiceControlThreadHandleOutputError: fPollEvt=0x%x, idPollHnd=%u\n",
332 fPollEvt, idPollHnd);
333#endif
334
335 /* Remove pipe from poll set. */
336 int rc2 = RTPollSetRemove(hPollSet, idPollHnd);
337 AssertMsg(RT_SUCCESS(rc2) || rc2 == VERR_POLL_HANDLE_ID_NOT_FOUND, ("%Rrc\n", rc2));
338
339 bool fClosePipe = true; /* By default close the pipe. */
340
341 /* Check if there's remaining data to read from the pipe. */
342 size_t cbReadable;
343 rc2 = RTPipeQueryReadable(*phPipeR, &cbReadable);
344 if ( RT_SUCCESS(rc2)
345 && cbReadable)
346 {
347 VBoxServiceVerbose(3, "VBoxServiceControlThreadHandleOutputError: idPollHnd=%u has %ld bytes left, vetoing close\n",
348 idPollHnd, cbReadable);
349
350 /* Veto closing the pipe yet because there's still stuff to read
351 * from the pipe. This can happen on UNIX-y systems where on
352 * error/hangup there still can be data to be read out. */
353 fClosePipe = false;
354 }
355 else
356 VBoxServiceVerbose(3, "VBoxServiceControlThreadHandleOutputError: idPollHnd=%u will be closed\n",
357 idPollHnd);
358
359 if ( *phPipeR != NIL_RTPIPE
360 && fClosePipe)
361 {
362 rc2 = RTPipeClose(*phPipeR);
363 AssertRC(rc2);
364 *phPipeR = NIL_RTPIPE;
365 }
366
367 return VINF_SUCCESS;
368}
369
370
371/**
372 * Handle pending output data or error on standard out or standard error.
373 *
374 * @returns IPRT status code from client send.
375 * @param hPollSet The polling set.
376 * @param fPollEvt The event mask returned by RTPollNoResume.
377 * @param phPipeR The pipe handle.
378 * @param idPollHnd The pipe ID to handle.
379 *
380 */
381static int VBoxServiceControlThreadHandleOutputEvent(RTPOLLSET hPollSet, uint32_t fPollEvt,
382 PRTPIPE phPipeR, uint32_t idPollHnd)
383{
384#if 0
385 VBoxServiceVerbose(4, "VBoxServiceControlThreadHandleOutputEvent: fPollEvt=0x%x, idPollHnd=%u\n",
386 fPollEvt, idPollHnd);
387#endif
388
389 int rc = VINF_SUCCESS;
390
391#ifdef DEBUG
392 size_t cbReadable;
393 rc = RTPipeQueryReadable(*phPipeR, &cbReadable);
394 if ( RT_SUCCESS(rc)
395 && cbReadable)
396 {
397 VBoxServiceVerbose(4, "VBoxServiceControlThreadHandleOutputEvent: cbReadable=%ld\n",
398 cbReadable);
399 }
400#endif
401
402#if 0
403 //if (fPollEvt & RTPOLL_EVT_READ)
404 {
405 size_t cbRead = 0;
406 uint8_t byData[_64K];
407 rc = RTPipeRead(*phPipeR,
408 byData, sizeof(byData), &cbRead);
409 VBoxServiceVerbose(4, "VBoxServiceControlThreadHandleOutputEvent cbRead=%u, rc=%Rrc\n",
410 cbRead, rc);
411
412 /* Make sure we go another poll round in case there was too much data
413 for the buffer to hold. */
414 fPollEvt &= RTPOLL_EVT_ERROR;
415 }
416#endif
417
418 if (fPollEvt & RTPOLL_EVT_ERROR)
419 rc = VBoxServiceControlThreadHandleOutputError(hPollSet, fPollEvt,
420 phPipeR, idPollHnd);
421 return rc;
422}
423
424
425static int VBoxServiceControlThreadHandleRequest(RTPOLLSET hPollSet, uint32_t fPollEvt,
426 PRTPIPE phStdInW, PRTPIPE phStdOutR, PRTPIPE phStdErrR,
427 PVBOXSERVICECTRLTHREAD pThread)
428{
429 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
430 AssertPtrReturn(phStdInW, VERR_INVALID_POINTER);
431 AssertPtrReturn(phStdOutR, VERR_INVALID_POINTER);
432 AssertPtrReturn(phStdErrR, VERR_INVALID_POINTER);
433
434 /* Drain the notification pipe. */
435 uint8_t abBuf[8];
436 size_t cbIgnore;
437 int rc = RTPipeRead(pThread->hNotificationPipeR, abBuf, sizeof(abBuf), &cbIgnore);
438 if (RT_FAILURE(rc))
439 VBoxServiceError("Draining IPC notification pipe failed with rc=%Rrc\n", rc);
440
441 int rcReq = VINF_SUCCESS; /* Actual request result. */
442
443 PVBOXSERVICECTRLREQUEST pRequest = pThread->pRequest;
444 if (!pRequest)
445 {
446 VBoxServiceError("IPC request is invalid\n");
447 return VERR_INVALID_POINTER;
448 }
449
450 switch (pRequest->enmType)
451 {
452 case VBOXSERVICECTRLREQUEST_QUIT: /* Main control asked us to quit. */
453 {
454 /** @todo Check for some conditions to check to
455 * veto quitting. */
456 ASMAtomicXchgBool(&pThread->fShutdown, true);
457 rcReq = VERR_CANCELLED;
458 break;
459 }
460
461 case VBOXSERVICECTRLREQUEST_STDIN_WRITE:
462 case VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF:
463 {
464 size_t cbWritten = 0;
465 if (pRequest->cbData)
466 {
467 AssertPtrReturn(pRequest->pvData, VERR_INVALID_POINTER);
468 if (*phStdInW != NIL_RTPIPE)
469 {
470 rcReq = RTPipeWrite(*phStdInW,
471 pRequest->pvData, pRequest->cbData, &cbWritten);
472 }
473 else
474 rcReq = VINF_EOF;
475 }
476
477 /*
478 * If this is the last write + we have really have written all data
479 * we need to close the stdin pipe on our end and remove it from
480 * the poll set.
481 */
482 if ( pRequest->enmType == VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF
483 && pRequest->cbData == cbWritten)
484 {
485 rc = VBoxServiceControlThreadCloseStdIn(hPollSet, phStdInW);
486 }
487
488 /* Report back actual data written (if any). */
489 pRequest->cbData = cbWritten;
490 break;
491 }
492
493 case VBOXSERVICECTRLREQUEST_STDOUT_READ:
494 case VBOXSERVICECTRLREQUEST_STDERR_READ:
495 {
496 AssertPtrReturn(pRequest->pvData, VERR_INVALID_POINTER);
497 AssertReturn(pRequest->cbData, VERR_INVALID_PARAMETER);
498
499 PRTPIPE pPipeR = pRequest->enmType == VBOXSERVICECTRLREQUEST_STDERR_READ
500 ? phStdErrR : phStdOutR;
501 AssertPtr(pPipeR);
502
503 size_t cbRead = 0;
504 if (*pPipeR != NIL_RTPIPE)
505 {
506 rcReq = RTPipeRead(*pPipeR,
507 pRequest->pvData, pRequest->cbData, &cbRead);
508 if (RT_FAILURE(rcReq))
509 {
510 RTPollSetRemove(hPollSet, pRequest->enmType == VBOXSERVICECTRLREQUEST_STDERR_READ
511 ? VBOXSERVICECTRLPIPEID_STDERR : VBOXSERVICECTRLPIPEID_STDOUT);
512 RTPipeClose(*pPipeR);
513 *pPipeR = NIL_RTPIPE;
514 if (rcReq == VERR_BROKEN_PIPE)
515 rcReq = VINF_EOF;
516 }
517 }
518 else
519 rcReq = VINF_EOF;
520
521 /* Report back actual data read (if any). */
522 pRequest->cbData = cbRead;
523 break;
524 }
525
526 default:
527 rcReq = VERR_NOT_IMPLEMENTED;
528 break;
529 }
530
531 /* Assign overall result. */
532 pRequest->rc = RT_SUCCESS(rc)
533 ? rcReq : rc;
534
535 VBoxServiceVerbose(2, "[PID %u]: Handled req=%u, CID=%u, rc=%Rrc, cbData=%u\n",
536 pThread->uPID, pRequest->enmType, pRequest->uCID, pRequest->rc, pRequest->cbData);
537
538 /* In any case, regardless of the result, we notify
539 * the main guest control to unblock it. */
540 int rc2 = RTSemEventMultiSignal(pRequest->Event);
541 AssertRC(rc2);
542
543 /* No access to pRequest here anymore -- could be out of scope
544 * or modified already! */
545 pThread->pRequest = pRequest = NULL;
546
547 return rc;
548}
549
550
551/**
552 * Execution loop which runs in a dedicated per-started-process thread and
553 * handles all pipe input/output and signalling stuff.
554 *
555 * @return IPRT status code.
556 * @param pThread The process' thread handle.
557 * @param hProcess The actual process handle.
558 * @param cMsTimeout Time limit (in ms) of the process' life time.
559 * @param hPollSet The poll set to use.
560 * @param hStdInW Handle to the process' stdin write end.
561 * @param hStdOutR Handle to the process' stdout read end.
562 * @param hStdErrR Handle to the process' stderr read end.
563 */
564static int VBoxServiceControlThreadProcLoop(PVBOXSERVICECTRLTHREAD pThread,
565 RTPROCESS hProcess, RTMSINTERVAL cMsTimeout, RTPOLLSET hPollSet,
566 PRTPIPE phStdInW, PRTPIPE phStdOutR, PRTPIPE phStdErrR)
567{
568 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
569 AssertPtrReturn(phStdInW, VERR_INVALID_PARAMETER);
570 /* Rest is optional. */
571
572 int rc;
573 int rc2;
574 uint64_t const MsStart = RTTimeMilliTS();
575 RTPROCSTATUS ProcessStatus = { 254, RTPROCEXITREASON_ABEND };
576 bool fProcessAlive = true;
577 bool fProcessTimedOut = false;
578 uint64_t MsProcessKilled = UINT64_MAX;
579 RTMSINTERVAL const cMsPollBase = *phStdInW != NIL_RTPIPE
580 ? 100 /* Need to poll for input. */
581 : 1000; /* Need only poll for process exit and aborts. */
582 RTMSINTERVAL cMsPollCur = 0;
583
584 /*
585 * Assign PID to thread data.
586 * Also check if there already was a thread with the same PID and shut it down -- otherwise
587 * the first (stale) entry will be found and we get really weird results!
588 */
589 rc = VBoxServiceControlAssignPID(pThread, hProcess);
590 if (RT_FAILURE(rc))
591 {
592 VBoxServiceError("Unable to assign PID=%u, to new thread, rc=%Rrc\n",
593 hProcess, rc);
594 return rc;
595 }
596
597 /*
598 * Before entering the loop, tell the host that we've started the guest
599 * and that it's now OK to send input to the process.
600 */
601 VBoxServiceVerbose(2, "[PID %u]: Process \"%s\" started, CID=%u, User=%s\n",
602 pThread->uPID, pThread->pszCmd, pThread->uContextID, pThread->pszUser);
603 rc = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID,
604 pThread->uPID, PROC_STS_STARTED, 0 /* u32Flags */,
605 NULL /* pvData */, 0 /* cbData */);
606
607 /*
608 * Process input, output, the test pipe and client requests.
609 */
610 while ( RT_SUCCESS(rc)
611 && RT_UNLIKELY(!pThread->fShutdown))
612 {
613 /*
614 * Wait/Process all pending events.
615 */
616 uint32_t idPollHnd;
617 uint32_t fPollEvt;
618 rc2 = RTPollNoResume(hPollSet, cMsPollCur, &fPollEvt, &idPollHnd);
619 if (pThread->fShutdown)
620 continue;
621
622 cMsPollCur = 0; /* No rest until we've checked everything. */
623
624 if (RT_SUCCESS(rc2))
625 {
626 /*VBoxServiceVerbose(4, "[PID %u}: RTPollNoResume idPollHnd=%u\n",
627 pThread->uPID, idPollHnd);*/
628 switch (idPollHnd)
629 {
630 case VBOXSERVICECTRLPIPEID_STDIN:
631 rc = VBoxServiceControlThreadHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW);
632 break;
633
634 case VBOXSERVICECTRLPIPEID_STDOUT:
635 rc = VBoxServiceControlThreadHandleOutputEvent(hPollSet, fPollEvt,
636 phStdOutR, idPollHnd);
637 break;
638
639 case VBOXSERVICECTRLPIPEID_STDERR:
640 rc = VBoxServiceControlThreadHandleOutputEvent(hPollSet, fPollEvt,
641 phStdErrR, idPollHnd);
642 break;
643
644 case VBOXSERVICECTRLPIPEID_IPC_NOTIFY:
645 rc = VBoxServiceControlThreadHandleRequest(hPollSet, fPollEvt,
646 phStdInW, phStdOutR, phStdErrR, pThread);
647 break;
648 }
649
650 if (RT_FAILURE(rc) || rc == VINF_EOF)
651 break; /* Abort command, or client dead or something. */
652
653 if (RT_UNLIKELY(pThread->fShutdown))
654 break; /* We were asked to shutdown. */
655
656 continue;
657 }
658
659#if 0
660 VBoxServiceVerbose(4, "[PID %u]: Polling done, pollRC=%Rrc, pollCnt=%u, rc=%Rrc, fShutdown=%RTbool\n",
661 pThread->uPID, rc2, RTPollSetGetCount(hPollSet), rc, pThread->fShutdown);
662#endif
663 /*
664 * Check for process death.
665 */
666 if (fProcessAlive)
667 {
668 rc2 = RTProcWaitNoResume(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
669 if (RT_SUCCESS_NP(rc2))
670 {
671 fProcessAlive = false;
672 continue;
673 }
674 if (RT_UNLIKELY(rc2 == VERR_INTERRUPTED))
675 continue;
676 if (RT_UNLIKELY(rc2 == VERR_PROCESS_NOT_FOUND))
677 {
678 fProcessAlive = false;
679 ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
680 ProcessStatus.iStatus = 255;
681 AssertFailed();
682 }
683 else
684 AssertMsg(rc2 == VERR_PROCESS_RUNNING, ("%Rrc\n", rc2));
685 }
686
687 /*
688 * If the process has terminated and all output has been consumed,
689 * we should be heading out.
690 */
691 if ( !fProcessAlive
692 && *phStdOutR == NIL_RTPIPE
693 && *phStdErrR == NIL_RTPIPE)
694 break;
695
696 /*
697 * Check for timed out, killing the process.
698 */
699 uint32_t cMilliesLeft = RT_INDEFINITE_WAIT;
700 if (cMsTimeout != RT_INDEFINITE_WAIT)
701 {
702 uint64_t u64Now = RTTimeMilliTS();
703 uint64_t cMsElapsed = u64Now - MsStart;
704 if (cMsElapsed >= cMsTimeout)
705 {
706 VBoxServiceVerbose(3, "[PID %u]: Timed out (%ums elapsed > %ums timeout), killing ...",
707 pThread->uPID, cMsElapsed, cMsTimeout);
708
709 fProcessTimedOut = true;
710 if ( MsProcessKilled == UINT64_MAX
711 || u64Now - MsProcessKilled > 1000)
712 {
713 if (u64Now - MsProcessKilled > 20*60*1000)
714 break; /* Give up after 20 mins. */
715 RTProcTerminate(hProcess);
716 MsProcessKilled = u64Now;
717 continue;
718 }
719 cMilliesLeft = 10000;
720 }
721 else
722 cMilliesLeft = cMsTimeout - (uint32_t)cMsElapsed;
723 }
724
725 /* Reset the polling interval since we've done all pending work. */
726 cMsPollCur = fProcessAlive
727 ? cMsPollBase
728 : RT_MS_1MIN;
729 if (cMilliesLeft < cMsPollCur)
730 cMsPollCur = cMilliesLeft;
731
732 /*
733 * Need to exit?
734 */
735 if (pThread->fShutdown)
736 break;
737 }
738
739 rc2 = RTCritSectEnter(&pThread->CritSect);
740 if (RT_SUCCESS(rc2))
741 {
742 ASMAtomicXchgBool(&pThread->fShutdown, true);
743
744 rc2 = RTCritSectLeave(&pThread->CritSect);
745 AssertRC(rc2);
746 }
747
748 /*
749 * Try kill the process if it's still alive at this point.
750 */
751 if (fProcessAlive)
752 {
753 if (MsProcessKilled == UINT64_MAX)
754 {
755 VBoxServiceVerbose(3, "[PID %u]: Is still alive and not killed yet\n",
756 pThread->uPID);
757
758 MsProcessKilled = RTTimeMilliTS();
759 RTProcTerminate(hProcess);
760 RTThreadSleep(500);
761 }
762
763 for (size_t i = 0; i < 10; i++)
764 {
765 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Waiting to exit ...\n",
766 pThread->uPID, i + 1);
767 rc2 = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
768 if (RT_SUCCESS(rc2))
769 {
770 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Exited\n",
771 pThread->uPID, i + 1);
772 fProcessAlive = false;
773 break;
774 }
775 if (i >= 5)
776 {
777 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Trying to terminate ...\n",
778 pThread->uPID, i + 1);
779 RTProcTerminate(hProcess);
780 }
781 RTThreadSleep(i >= 5 ? 2000 : 500);
782 }
783
784 if (fProcessAlive)
785 VBoxServiceVerbose(3, "[PID %u]: Could not be killed\n", pThread->uPID);
786 }
787
788 /*
789 * If we don't have a client problem (RT_FAILURE(rc)) we'll reply to the
790 * clients exec packet now.
791 */
792 if (RT_SUCCESS(rc))
793 {
794 uint32_t uStatus = PROC_STS_UNDEFINED;
795 uint32_t uFlags = 0;
796
797 if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX)
798 {
799 VBoxServiceVerbose(3, "[PID %u]: Timed out and got killed\n",
800 pThread->uPID);
801 uStatus = PROC_STS_TOK;
802 }
803 else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX)
804 {
805 VBoxServiceVerbose(3, "[PID %u]: Timed out and did *not* get killed\n",
806 pThread->uPID);
807 uStatus = PROC_STS_TOA;
808 }
809 else if (pThread->fShutdown && (fProcessAlive || MsProcessKilled != UINT64_MAX))
810 {
811 VBoxServiceVerbose(3, "[PID %u]: Got terminated because system/service is about to shutdown\n",
812 pThread->uPID);
813 uStatus = PROC_STS_DWN; /* Service is stopping, process was killed. */
814 uFlags = pThread->uFlags; /* Return handed-in execution flags back to the host. */
815 }
816 else if (fProcessAlive)
817 {
818 VBoxServiceError("[PID %u]: Is alive when it should not!\n",
819 pThread->uPID);
820 }
821 else if (MsProcessKilled != UINT64_MAX)
822 {
823 VBoxServiceError("[PID %u]: Has been killed when it should not!\n",
824 pThread->uPID);
825 }
826 else if (ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL)
827 {
828 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_NORMAL (Exit code: %u)\n",
829 pThread->uPID, ProcessStatus.iStatus);
830
831 uStatus = PROC_STS_TEN;
832 uFlags = ProcessStatus.iStatus;
833 }
834 else if (ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL)
835 {
836 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_SIGNAL (Signal: %u)\n",
837 pThread->uPID, ProcessStatus.iStatus);
838
839 uStatus = PROC_STS_TES;
840 uFlags = ProcessStatus.iStatus;
841 }
842 else if (ProcessStatus.enmReason == RTPROCEXITREASON_ABEND)
843 {
844 /* ProcessStatus.iStatus will be undefined. */
845 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_ABEND\n",
846 pThread->uPID);
847
848 uStatus = PROC_STS_TEA;
849 uFlags = ProcessStatus.iStatus;
850 }
851 else
852 VBoxServiceVerbose(1, "[PID %u]: Handling process status %u not implemented\n",
853 pThread->uPID, ProcessStatus.enmReason);
854
855 VBoxServiceVerbose(2, "[PID %u]: Ended, ClientID=%u, CID=%u, Status=%u, Flags=0x%x\n",
856 pThread->uPID, pThread->uClientID, pThread->uContextID, uStatus, uFlags);
857 rc = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID,
858 pThread->uPID, uStatus, uFlags,
859 NULL /* pvData */, 0 /* cbData */);
860 if (RT_FAILURE(rc))
861 VBoxServiceError("[PID %u]: Error reporting final status to host; rc=%Rrc\n",
862 pThread->uPID, rc);
863
864 VBoxServiceVerbose(3, "[PID %u]: Process loop ended with rc=%Rrc\n",
865 pThread->uPID, rc);
866 }
867 else
868 VBoxServiceError("[PID %u]: Loop failed with rc=%Rrc\n",
869 pThread->uPID, rc);
870 return rc;
871}
872
873
874/**
875 * Initializes a pipe's handle and pipe object.
876 *
877 * @return IPRT status code.
878 * @param ph The pipe's handle to initialize.
879 * @param phPipe The pipe's object to initialize.
880 */
881static int vboxServiceControlThreadInitPipe(PRTHANDLE ph, PRTPIPE phPipe)
882{
883 AssertPtrReturn(ph, VERR_INVALID_PARAMETER);
884 AssertPtrReturn(phPipe, VERR_INVALID_PARAMETER);
885
886 ph->enmType = RTHANDLETYPE_PIPE;
887 ph->u.hPipe = NIL_RTPIPE;
888 *phPipe = NIL_RTPIPE;
889
890 return VINF_SUCCESS;
891}
892
893
894/**
895 * Allocates a guest thread request with the specified request data.
896 *
897 * @return IPRT status code.
898 * @param ppReq Pointer that will receive the newly allocated request.
899 * Must be freed later with VBoxServiceControlThreadRequestFree().
900 * @param enmType Request type.
901 * @param pbData Payload data, based on request type.
902 * @param cbData Size of payload data (in bytes).
903 * @param uCID Context ID to which this request belongs to.
904 */
905int VBoxServiceControlThreadRequestAllocEx(PVBOXSERVICECTRLREQUEST *ppReq,
906 VBOXSERVICECTRLREQUESTTYPE enmType,
907 void* pbData,
908 size_t cbData,
909 uint32_t uCID)
910{
911 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
912
913 PVBOXSERVICECTRLREQUEST pReq = (PVBOXSERVICECTRLREQUEST)
914 RTMemAlloc(sizeof(VBOXSERVICECTRLREQUEST));
915 AssertPtrReturn(pReq, VERR_NO_MEMORY);
916
917 RT_ZERO(*pReq);
918 pReq->enmType = enmType;
919 pReq->uCID = uCID;
920 pReq->cbData = cbData;
921 pReq->pvData = (uint8_t*)pbData;
922
923 /* Set request result to some defined state in case
924 * it got cancelled. */
925 pReq->rc = VERR_CANCELLED;
926
927 int rc = RTSemEventMultiCreate(&pReq->Event);
928 AssertRC(rc);
929
930 if (RT_SUCCESS(rc))
931 {
932 *ppReq = pReq;
933 return VINF_SUCCESS;
934 }
935
936 RTMemFree(pReq);
937 return rc;
938}
939
940
941/**
942 * Allocates a guest thread request with the specified request data.
943 *
944 * @return IPRT status code.
945 * @param ppReq Pointer that will receive the newly allocated request.
946 * Must be freed later with VBoxServiceControlThreadRequestFree().
947 * @param enmType Request type.
948 */
949int VBoxServiceControlThreadRequestAlloc(PVBOXSERVICECTRLREQUEST *ppReq,
950 VBOXSERVICECTRLREQUESTTYPE enmType)
951{
952 return VBoxServiceControlThreadRequestAllocEx(ppReq, enmType,
953 NULL /* pvData */, 0 /* cbData */,
954 0 /* ContextID */);
955}
956
957
958/**
959 * Cancels a previously fired off guest thread request.
960 *
961 * Note: Does *not* do locking since VBoxServiceControlThreadRequestWait()
962 * holds the lock (critsect); so only trigger the signal; the owner
963 * needs to clean up afterwards.
964 *
965 * @return IPRT status code.
966 * @param pReq Request to cancel.
967 */
968static int vboxServiceControlThreadRequestCancel(PVBOXSERVICECTRLREQUEST pReq)
969{
970 if (!pReq) /* Silently skip non-initialized requests. */
971 return VINF_SUCCESS;
972
973 VBoxServiceVerbose(4, "Cancelling request=0x%p\n", pReq);
974
975 return RTSemEventMultiSignal(pReq->Event);
976}
977
978
979/**
980 * Frees a formerly allocated guest thread request.
981 *
982 * @return IPRT status code.
983 * @param pReq Request to free.
984 */
985void VBoxServiceControlThreadRequestFree(PVBOXSERVICECTRLREQUEST pReq)
986{
987 AssertPtrReturnVoid(pReq);
988
989 VBoxServiceVerbose(4, "Freeing request=0x%p (event=%RTsem)\n",
990 pReq, &pReq->Event);
991
992 int rc = RTSemEventMultiDestroy(pReq->Event);
993 AssertRC(rc);
994
995 RTMemFree(pReq);
996 pReq = NULL;
997}
998
999
1000/**
1001 * Waits for a guest thread's event to get triggered.
1002 *
1003 * @return IPRT status code.
1004 * @param pReq Request to wait for.
1005 */
1006int VBoxServiceControlThreadRequestWait(PVBOXSERVICECTRLREQUEST pReq)
1007{
1008 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1009
1010 /* Wait on the request to get completed (or we are asked to abort/shutdown). */
1011 int rc = RTSemEventMultiWait(pReq->Event, RT_INDEFINITE_WAIT);
1012 if (RT_SUCCESS(rc))
1013 {
1014 VBoxServiceVerbose(4, "Performed request with rc=%Rrc, cbData=%u\n",
1015 pReq->rc, pReq->cbData);
1016
1017 /* Give back overall request result. */
1018 rc = pReq->rc;
1019 }
1020 else
1021 VBoxServiceError("Waiting for request failed, rc=%Rrc\n", rc);
1022
1023 return rc;
1024}
1025
1026
1027/**
1028 * Sets up the redirection / pipe / nothing for one of the standard handles.
1029 *
1030 * @returns IPRT status code. No client replies made.
1031 * @param pszHowTo How to set up this standard handle.
1032 * @param fd Which standard handle it is (0 == stdin, 1 ==
1033 * stdout, 2 == stderr).
1034 * @param ph The generic handle that @a pph may be set
1035 * pointing to. Always set.
1036 * @param pph Pointer to the RTProcCreateExec argument.
1037 * Always set.
1038 * @param phPipe Where to return the end of the pipe that we
1039 * should service.
1040 */
1041static int VBoxServiceControlThreadSetupPipe(const char *pszHowTo, int fd,
1042 PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe)
1043{
1044 AssertPtrReturn(ph, VERR_INVALID_POINTER);
1045 AssertPtrReturn(pph, VERR_INVALID_POINTER);
1046 AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
1047
1048 int rc;
1049
1050 ph->enmType = RTHANDLETYPE_PIPE;
1051 ph->u.hPipe = NIL_RTPIPE;
1052 *pph = NULL;
1053 *phPipe = NIL_RTPIPE;
1054
1055 if (!strcmp(pszHowTo, "|"))
1056 {
1057 /*
1058 * Setup a pipe for forwarding to/from the client.
1059 * The ph union struct will be filled with a pipe read/write handle
1060 * to represent the "other" end to phPipe.
1061 */
1062 if (fd == 0) /* stdin? */
1063 {
1064 /* Connect a wrtie pipe specified by phPipe to stdin. */
1065 rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ);
1066 }
1067 else /* stdout or stderr? */
1068 {
1069 /* Connect a read pipe specified by phPipe to stdout or stderr. */
1070 rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE);
1071 }
1072
1073 if (RT_FAILURE(rc))
1074 return rc;
1075
1076 ph->enmType = RTHANDLETYPE_PIPE;
1077 *pph = ph;
1078 }
1079 else if (!strcmp(pszHowTo, "/dev/null"))
1080 {
1081 /*
1082 * Redirect to/from /dev/null.
1083 */
1084 RTFILE hFile;
1085 rc = RTFileOpenBitBucket(&hFile, fd == 0 ? RTFILE_O_READ : RTFILE_O_WRITE);
1086 if (RT_FAILURE(rc))
1087 return rc;
1088
1089 ph->enmType = RTHANDLETYPE_FILE;
1090 ph->u.hFile = hFile;
1091 *pph = ph;
1092 }
1093 else /* Add other piping stuff here. */
1094 rc = VINF_SUCCESS; /* Same as parent (us). */
1095
1096 return rc;
1097}
1098
1099
1100/**
1101 * Expands a file name / path to its real content. This only works on Windows
1102 * for now (e.g. translating "%TEMP%\foo.exe" to "C:\Windows\Temp" when starting
1103 * with system / administrative rights).
1104 *
1105 * @return IPRT status code.
1106 * @param pszPath Path to resolve.
1107 * @param pszExpanded Pointer to string to store the resolved path in.
1108 * @param cbExpanded Size (in bytes) of string to store the resolved path.
1109 */
1110static int VBoxServiceControlThreadMakeFullPath(const char *pszPath, char *pszExpanded, size_t cbExpanded)
1111{
1112 int rc = VINF_SUCCESS;
1113#ifdef RT_OS_WINDOWS
1114 if (!ExpandEnvironmentStrings(pszPath, pszExpanded, cbExpanded))
1115 rc = RTErrConvertFromWin32(GetLastError());
1116#else
1117 /* No expansion for non-Windows yet. */
1118 rc = RTStrCopy(pszExpanded, cbExpanded, pszPath);
1119#endif
1120#ifdef DEBUG
1121 VBoxServiceVerbose(3, "VBoxServiceControlExecMakeFullPath: %s -> %s\n",
1122 pszPath, pszExpanded);
1123#endif
1124 return rc;
1125}
1126
1127
1128/**
1129 * Resolves the full path of a specified executable name. This function also
1130 * resolves internal VBoxService tools to its appropriate executable path + name if
1131 * VBOXSERVICE_NAME is specified as pszFileName.
1132 *
1133 * @return IPRT status code.
1134 * @param pszFileName File name to resolve.
1135 * @param pszResolved Pointer to a string where the resolved file name will be stored.
1136 * @param cbResolved Size (in bytes) of resolved file name string.
1137 */
1138static int VBoxServiceControlThreadResolveExecutable(const char *pszFileName,
1139 char *pszResolved, size_t cbResolved)
1140{
1141 AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
1142 AssertPtrReturn(pszResolved, VERR_INVALID_POINTER);
1143 AssertReturn(cbResolved, VERR_INVALID_PARAMETER);
1144
1145 int rc = VINF_SUCCESS;
1146
1147 char szPathToResolve[RTPATH_MAX];
1148 if ( (g_pszProgName && (RTStrICmp(pszFileName, g_pszProgName) == 0))
1149 || !RTStrICmp(pszFileName, VBOXSERVICE_NAME))
1150 {
1151 /* Resolve executable name of this process. */
1152 if (!RTProcGetExecutablePath(szPathToResolve, sizeof(szPathToResolve)))
1153 rc = VERR_FILE_NOT_FOUND;
1154 }
1155 else
1156 {
1157 /* Take the raw argument to resolve. */
1158 rc = RTStrCopy(szPathToResolve, sizeof(szPathToResolve), pszFileName);
1159 }
1160
1161 if (RT_SUCCESS(rc))
1162 {
1163 rc = VBoxServiceControlThreadMakeFullPath(szPathToResolve, pszResolved, cbResolved);
1164 if (RT_SUCCESS(rc))
1165 VBoxServiceVerbose(3, "Looked up executable: %s -> %s\n",
1166 pszFileName, pszResolved);
1167 }
1168
1169 if (RT_FAILURE(rc))
1170 VBoxServiceError("Failed to lookup executable \"%s\" with rc=%Rrc\n",
1171 pszFileName, rc);
1172 return rc;
1173}
1174
1175
1176/**
1177 * Constructs the argv command line by resolving environment variables
1178 * and relative paths.
1179 *
1180 * @return IPRT status code.
1181 * @param pszArgv0 First argument (argv0), either original or modified version. Optional.
1182 * @param papszArgs Original argv command line from the host, starting at argv[1].
1183 * @param ppapszArgv Pointer to a pointer with the new argv command line.
1184 * Needs to be freed with RTGetOptArgvFree.
1185 */
1186static int VBoxServiceControlThreadAllocateArgv(const char *pszArgv0,
1187 const char * const *papszArgs,
1188 bool fExpandArgs, char ***ppapszArgv)
1189{
1190 AssertPtrReturn(ppapszArgv, VERR_INVALID_POINTER);
1191
1192 VBoxServiceVerbose(3, "VBoxServiceControlThreadPrepareArgv: pszArgv0=%p, papszArgs=%p, fExpandArgs=%RTbool, ppapszArgv=%p\n",
1193 pszArgv0, papszArgs, fExpandArgs, ppapszArgv);
1194
1195 int rc = VINF_SUCCESS;
1196 uint32_t cArgs;
1197 for (cArgs = 0; papszArgs[cArgs]; cArgs++)
1198 {
1199 if (cArgs >= UINT32_MAX - 2)
1200 return VERR_BUFFER_OVERFLOW;
1201 }
1202
1203 /* Allocate new argv vector (adding + 2 for argv0 + termination). */
1204 size_t cbSize = (cArgs + 2) * sizeof(char*);
1205 char **papszNewArgv = (char**)RTMemAlloc(cbSize);
1206 if (!papszNewArgv)
1207 return VERR_NO_MEMORY;
1208
1209#ifdef DEBUG
1210 VBoxServiceVerbose(3, "VBoxServiceControlThreadPrepareArgv: cbSize=%RU32, cArgs=%RU32\n",
1211 cbSize, cArgs);
1212#endif
1213
1214 size_t i = 0; /* Keep the argument counter in scope for cleaning up on failure. */
1215
1216 rc = RTStrDupEx(&papszNewArgv[0], pszArgv0);
1217 if (RT_SUCCESS(rc))
1218 {
1219 for (; i < cArgs; i++)
1220 {
1221 char *pszArg;
1222#if 0 /* Arguments expansion -- untested. */
1223 if (fExpandArgs)
1224 {
1225 /* According to MSDN the limit on older Windows version is 32K, whereas
1226 * Vista+ there are no limits anymore. We still stick to 4K. */
1227 char szExpanded[_4K];
1228# ifdef RT_OS_WINDOWS
1229 if (!ExpandEnvironmentStrings(papszArgs[i], szExpanded, sizeof(szExpanded)))
1230 rc = RTErrConvertFromWin32(GetLastError());
1231# else
1232 /* No expansion for non-Windows yet. */
1233 rc = RTStrCopy(papszArgs[i], sizeof(szExpanded), szExpanded);
1234# endif
1235 if (RT_SUCCESS(rc))
1236 rc = RTStrDupEx(&pszArg, szExpanded);
1237 }
1238 else
1239#endif
1240 rc = RTStrDupEx(&pszArg, papszArgs[i]);
1241
1242 if (RT_FAILURE(rc))
1243 break;
1244
1245 papszNewArgv[i + 1] = pszArg;
1246 }
1247
1248 if (RT_SUCCESS(rc))
1249 {
1250 /* Terminate array. */
1251 papszNewArgv[cArgs + 1] = NULL;
1252
1253 *ppapszArgv = papszNewArgv;
1254 }
1255 }
1256
1257 if (RT_FAILURE(rc))
1258 {
1259 for (i; i > 0; i--)
1260 RTStrFree(papszNewArgv[i]);
1261 RTMemFree(papszNewArgv);
1262 }
1263
1264 return rc;
1265}
1266
1267
1268void VBoxServiceControlThreadFreeArgv(char **papszArgv)
1269{
1270 if (papszArgv)
1271 {
1272 size_t i = 0;
1273 while (papszArgv[i])
1274 RTStrFree(papszArgv[i++]);
1275 RTMemFree(papszArgv);
1276 }
1277}
1278
1279
1280/**
1281 * Helper function to create/start a process on the guest.
1282 *
1283 * @return IPRT status code.
1284 * @param pszExec Full qualified path of process to start (without arguments).
1285 * @param papszArgs Pointer to array of command line arguments.
1286 * @param hEnv Handle to environment block to use.
1287 * @param fFlags Process execution flags.
1288 * @param phStdIn Handle for the process' stdin pipe.
1289 * @param phStdOut Handle for the process' stdout pipe.
1290 * @param phStdErr Handle for the process' stderr pipe.
1291 * @param pszAsUser User name (account) to start the process under.
1292 * @param pszPassword Password of the specified user.
1293 * @param phProcess Pointer which will receive the process handle after
1294 * successful process start.
1295 */
1296static int VBoxServiceControlThreadCreateProcess(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
1297 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
1298 const char *pszPassword, PRTPROCESS phProcess)
1299{
1300 AssertPtrReturn(pszExec, VERR_INVALID_PARAMETER);
1301 AssertPtrReturn(papszArgs, VERR_INVALID_PARAMETER);
1302 AssertPtrReturn(phProcess, VERR_INVALID_PARAMETER);
1303
1304 int rc = VINF_SUCCESS;
1305 char szExecExp[RTPATH_MAX];
1306
1307 /* Do we need to expand environment variables in arguments? */
1308 bool fExpandArgs = (fFlags & EXECUTEPROCESSFLAG_EXPAND_ARGUMENTS) ? true : false;
1309
1310#ifdef RT_OS_WINDOWS
1311 /*
1312 * If sysprep should be executed do this in the context of VBoxService, which
1313 * (usually, if started by SCM) has administrator rights. Because of that a UI
1314 * won't be shown (doesn't have a desktop).
1315 */
1316 if (!RTStrICmp(pszExec, "sysprep"))
1317 {
1318 /* Use a predefined sysprep path as default. */
1319 char szSysprepCmd[RTPATH_MAX] = "C:\\sysprep\\sysprep.exe";
1320
1321 /*
1322 * On Windows Vista (and up) sysprep is located in "system32\\sysprep\\sysprep.exe",
1323 * so detect the OS and use a different path.
1324 */
1325 OSVERSIONINFOEX OSInfoEx;
1326 RT_ZERO(OSInfoEx);
1327 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1328 if ( GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
1329 && OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
1330 && OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
1331 {
1332 rc = RTEnvGetEx(RTENV_DEFAULT, "windir", szSysprepCmd, sizeof(szSysprepCmd), NULL);
1333 if (RT_SUCCESS(rc))
1334 rc = RTPathAppend(szSysprepCmd, sizeof(szSysprepCmd), "system32\\sysprep\\sysprep.exe");
1335 }
1336
1337 if (RT_SUCCESS(rc))
1338 {
1339 char **papszArgsExp;
1340 rc = VBoxServiceControlThreadAllocateArgv(szSysprepCmd /* argv0 */, papszArgs,
1341 fExpandArgs, &papszArgsExp);
1342 if (RT_SUCCESS(rc))
1343 {
1344 rc = RTProcCreateEx(szSysprepCmd, papszArgsExp, hEnv, 0 /* fFlags */,
1345 phStdIn, phStdOut, phStdErr, NULL /* pszAsUser */,
1346 NULL /* pszPassword */, phProcess);
1347 VBoxServiceControlThreadFreeArgv(papszArgsExp);
1348 }
1349 }
1350
1351 if (RT_FAILURE(rc))
1352 VBoxServiceVerbose(3, "Starting sysprep returned rc=%Rrc\n", rc);
1353
1354 return rc;
1355 }
1356#endif /* RT_OS_WINDOWS */
1357
1358#ifdef VBOXSERVICE_TOOLBOX
1359 if (RTStrStr(pszExec, "vbox_") == pszExec)
1360 {
1361 /* We want to use the internal toolbox (all internal
1362 * tools are starting with "vbox_" (e.g. "vbox_cat"). */
1363 rc = VBoxServiceControlThreadResolveExecutable(VBOXSERVICE_NAME, szExecExp, sizeof(szExecExp));
1364 }
1365 else
1366 {
1367#endif
1368 /*
1369 * Do the environment variables expansion on executable and arguments.
1370 */
1371 rc = VBoxServiceControlThreadResolveExecutable(pszExec, szExecExp, sizeof(szExecExp));
1372#ifdef VBOXSERVICE_TOOLBOX
1373 }
1374#endif
1375 if (RT_SUCCESS(rc))
1376 {
1377 char **papszArgsExp;
1378 rc = VBoxServiceControlThreadAllocateArgv(pszExec /* Always use the unmodified executable name as argv0. */,
1379 papszArgs /* Append the rest of the argument vector (if any). */,
1380 fExpandArgs, &papszArgsExp);
1381 if (RT_FAILURE(rc))
1382 {
1383 /* Don't print any arguments -- may contain passwords or other sensible data! */
1384 VBoxServiceError("Could not prepare arguments, rc=%Rrc\n", rc);
1385 }
1386 else
1387 {
1388 uint32_t uProcFlags = 0;
1389 if (fFlags)
1390 {
1391 if (fFlags & EXECUTEPROCESSFLAG_HIDDEN)
1392 uProcFlags |= RTPROC_FLAGS_HIDDEN;
1393 if (fFlags & EXECUTEPROCESSFLAG_NO_PROFILE)
1394 uProcFlags |= RTPROC_FLAGS_NO_PROFILE;
1395 }
1396
1397 /* If no user name specified run with current credentials (e.g.
1398 * full service/system rights). This is prohibited via official Main API!
1399 *
1400 * Otherwise use the RTPROC_FLAGS_SERVICE to use some special authentication
1401 * code (at least on Windows) for running processes as different users
1402 * started from our system service. */
1403 if (*pszAsUser)
1404 uProcFlags |= RTPROC_FLAGS_SERVICE;
1405#ifdef DEBUG
1406 VBoxServiceVerbose(3, "Command: %s\n", szExecExp);
1407 for (size_t i = 0; papszArgsExp[i]; i++)
1408 VBoxServiceVerbose(3, "\targv[%ld]: %s\n", i, papszArgsExp[i]);
1409#endif
1410 VBoxServiceVerbose(3, "Starting process \"%s\" ...\n", szExecExp);
1411
1412 /* Do normal execution. */
1413 rc = RTProcCreateEx(szExecExp, papszArgsExp, hEnv, uProcFlags,
1414 phStdIn, phStdOut, phStdErr,
1415 *pszAsUser ? pszAsUser : NULL,
1416 *pszPassword ? pszPassword : NULL,
1417 phProcess);
1418
1419 VBoxServiceVerbose(3, "Starting process \"%s\" returned rc=%Rrc\n",
1420 szExecExp, rc);
1421
1422 VBoxServiceControlThreadFreeArgv(papszArgsExp);
1423 }
1424 }
1425 return rc;
1426}
1427
1428/**
1429 * The actual worker routine (loop) for a started guest process.
1430 *
1431 * @return IPRT status code.
1432 * @param PVBOXSERVICECTRLTHREAD Thread data associated with a started process.
1433 */
1434static int VBoxServiceControlThreadProcessWorker(PVBOXSERVICECTRLTHREAD pThread)
1435{
1436 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
1437 VBoxServiceVerbose(3, "Thread of process pThread=0x%p = \"%s\" started\n",
1438 pThread, pThread->pszCmd);
1439
1440 int rc = VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLIST_RUNNING, pThread);
1441 AssertRC(rc);
1442
1443 rc = VbglR3GuestCtrlConnect(&pThread->uClientID);
1444 if (RT_FAILURE(rc))
1445 {
1446 VBoxServiceError("Thread failed to connect to the guest control service, aborted! Error: %Rrc\n", rc);
1447 RTThreadUserSignal(RTThreadSelf());
1448 return rc;
1449 }
1450 VBoxServiceVerbose(3, "Guest process \"%s\" got client ID=%u, flags=0x%x\n",
1451 pThread->pszCmd, pThread->uClientID, pThread->uFlags);
1452
1453 bool fSignalled = false; /* Indicator whether we signalled the thread user event already. */
1454
1455 /*
1456 * Create the environment.
1457 */
1458 RTENV hEnv;
1459 rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
1460 if (RT_SUCCESS(rc))
1461 {
1462 size_t i;
1463 for (i = 0; i < pThread->uNumEnvVars && pThread->papszEnv; i++)
1464 {
1465 rc = RTEnvPutEx(hEnv, pThread->papszEnv[i]);
1466 if (RT_FAILURE(rc))
1467 break;
1468 }
1469 if (RT_SUCCESS(rc))
1470 {
1471 /*
1472 * Setup the redirection of the standard stuff.
1473 */
1474 /** @todo consider supporting: gcc stuff.c >file 2>&1. */
1475 RTHANDLE hStdIn;
1476 PRTHANDLE phStdIn;
1477 rc = VBoxServiceControlThreadSetupPipe("|", 0 /*STDIN_FILENO*/,
1478 &hStdIn, &phStdIn, &pThread->pipeStdInW);
1479 if (RT_SUCCESS(rc))
1480 {
1481 RTHANDLE hStdOut;
1482 PRTHANDLE phStdOut;
1483 RTPIPE pipeStdOutR;
1484 rc = VBoxServiceControlThreadSetupPipe( (pThread->uFlags & EXECUTEPROCESSFLAG_WAIT_STDOUT)
1485 ? "|" : "/dev/null",
1486 1 /*STDOUT_FILENO*/,
1487 &hStdOut, &phStdOut, &pipeStdOutR);
1488 if (RT_SUCCESS(rc))
1489 {
1490 RTHANDLE hStdErr;
1491 PRTHANDLE phStdErr;
1492 RTPIPE pipeStdErrR;
1493 rc = VBoxServiceControlThreadSetupPipe( (pThread->uFlags & EXECUTEPROCESSFLAG_WAIT_STDERR)
1494 ? "|" : "/dev/null",
1495 2 /*STDERR_FILENO*/,
1496 &hStdErr, &phStdErr, &pipeStdErrR);
1497 if (RT_SUCCESS(rc))
1498 {
1499 /*
1500 * Create a poll set for the pipes and let the
1501 * transport layer add stuff to it as well.
1502 */
1503 RTPOLLSET hPollSet;
1504 rc = RTPollSetCreate(&hPollSet);
1505 if (RT_SUCCESS(rc))
1506 {
1507 uint32_t uFlags = RTPOLL_EVT_ERROR;
1508#if 0
1509 /* Add reading event to pollset to get some more information. */
1510 uFlags |= RTPOLL_EVT_READ;
1511#endif
1512 /* Stdin. */
1513 if (RT_SUCCESS(rc))
1514 rc = RTPollSetAddPipe(hPollSet, pThread->pipeStdInW, RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDIN);
1515 /* Stdout. */
1516 if (RT_SUCCESS(rc))
1517 rc = RTPollSetAddPipe(hPollSet, pipeStdOutR, uFlags, VBOXSERVICECTRLPIPEID_STDOUT);
1518 /* Stderr. */
1519 if (RT_SUCCESS(rc))
1520 rc = RTPollSetAddPipe(hPollSet, pipeStdErrR, uFlags, VBOXSERVICECTRLPIPEID_STDERR);
1521 /* IPC notification pipe. */
1522 if (RT_SUCCESS(rc))
1523 rc = RTPipeCreate(&pThread->hNotificationPipeR, &pThread->hNotificationPipeW, 0 /* Flags */);
1524 if (RT_SUCCESS(rc))
1525 rc = RTPollSetAddPipe(hPollSet, pThread->hNotificationPipeR, RTPOLL_EVT_READ, VBOXSERVICECTRLPIPEID_IPC_NOTIFY);
1526
1527 if (RT_SUCCESS(rc))
1528 {
1529 RTPROCESS hProcess;
1530 rc = VBoxServiceControlThreadCreateProcess(pThread->pszCmd, pThread->papszArgs, hEnv, pThread->uFlags,
1531 phStdIn, phStdOut, phStdErr,
1532 pThread->pszUser, pThread->pszPassword,
1533 &hProcess);
1534 if (RT_FAILURE(rc))
1535 VBoxServiceError("Error starting process, rc=%Rrc\n", rc);
1536 /*
1537 * Tell the control thread that it can continue
1538 * spawning services. This needs to be done after the new
1539 * process has been started because otherwise signal handling
1540 * on (Open) Solaris does not work correctly (see @bugref{5068}).
1541 */
1542 int rc2 = RTThreadUserSignal(RTThreadSelf());
1543 if (RT_SUCCESS(rc))
1544 rc = rc2;
1545 fSignalled = true;
1546
1547 if (RT_SUCCESS(rc))
1548 {
1549 /*
1550 * Close the child ends of any pipes and redirected files.
1551 */
1552 rc2 = RTHandleClose(phStdIn); AssertRC(rc2);
1553 phStdIn = NULL;
1554 rc2 = RTHandleClose(phStdOut); AssertRC(rc2);
1555 phStdOut = NULL;
1556 rc2 = RTHandleClose(phStdErr); AssertRC(rc2);
1557 phStdErr = NULL;
1558
1559 /* Enter the process loop. */
1560 rc = VBoxServiceControlThreadProcLoop(pThread,
1561 hProcess, pThread->uTimeLimitMS, hPollSet,
1562 &pThread->pipeStdInW, &pipeStdOutR, &pipeStdErrR);
1563
1564 /*
1565 * The handles that are no longer in the set have
1566 * been closed by the above call in order to prevent
1567 * the guest from getting stuck accessing them.
1568 * So, NIL the handles to avoid closing them again.
1569 */
1570 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_IPC_NOTIFY, NULL)))
1571 {
1572 pThread->hNotificationPipeR = NIL_RTPIPE;
1573 pThread->hNotificationPipeW = NIL_RTPIPE;
1574 }
1575 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDERR, NULL)))
1576 pipeStdErrR = NIL_RTPIPE;
1577 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDOUT, NULL)))
1578 pipeStdOutR = NIL_RTPIPE;
1579 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDIN, NULL)))
1580 pThread->pipeStdInW = NIL_RTPIPE;
1581 }
1582 }
1583 RTPollSetDestroy(hPollSet);
1584
1585 RTPipeClose(pThread->hNotificationPipeR);
1586 pThread->hNotificationPipeR = NIL_RTPIPE;
1587 RTPipeClose(pThread->hNotificationPipeW);
1588 pThread->hNotificationPipeW = NIL_RTPIPE;
1589 }
1590 RTPipeClose(pipeStdErrR);
1591 pipeStdErrR = NIL_RTPIPE;
1592 RTHandleClose(phStdErr);
1593 if (phStdErr)
1594 RTHandleClose(phStdErr);
1595 }
1596 RTPipeClose(pipeStdOutR);
1597 pipeStdOutR = NIL_RTPIPE;
1598 RTHandleClose(&hStdOut);
1599 if (phStdOut)
1600 RTHandleClose(phStdOut);
1601 }
1602 RTPipeClose(pThread->pipeStdInW);
1603 pThread->pipeStdInW = NIL_RTPIPE;
1604 RTHandleClose(phStdIn);
1605 }
1606 }
1607 RTEnvDestroy(hEnv);
1608 }
1609
1610 /* Move thread to stopped thread list. */
1611 int rc2 = VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLIST_STOPPED, pThread);
1612 AssertRC(rc2);
1613
1614 if (pThread->uClientID)
1615 {
1616 if (RT_FAILURE(rc))
1617 {
1618 rc2 = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID, pThread->uPID,
1619 PROC_STS_ERROR, rc,
1620 NULL /* pvData */, 0 /* cbData */);
1621 if (RT_FAILURE(rc2))
1622 VBoxServiceError("Could not report process failure error; rc=%Rrc (process error %Rrc)\n",
1623 rc2, rc);
1624 }
1625
1626 VBoxServiceVerbose(3, "[PID %u]: Cancelling pending host requests (client ID=%u)\n",
1627 pThread->uPID, pThread->uClientID);
1628 rc2 = VbglR3GuestCtrlCancelPendingWaits(pThread->uClientID);
1629 if (RT_FAILURE(rc2))
1630 {
1631 VBoxServiceError("[PID %u]: Cancelling pending host requests failed; rc=%Rrc\n",
1632 pThread->uPID, rc2);
1633 if (RT_SUCCESS(rc))
1634 rc = rc2;
1635 }
1636
1637 /* Disconnect from guest control service. */
1638 VBoxServiceVerbose(3, "[PID %u]: Disconnecting (client ID=%u) ...\n",
1639 pThread->uPID, pThread->uClientID);
1640 VbglR3GuestCtrlDisconnect(pThread->uClientID);
1641 pThread->uClientID = 0;
1642 }
1643
1644 VBoxServiceVerbose(3, "[PID %u]: Thread of process \"%s\" ended with rc=%Rrc\n",
1645 pThread->uPID, pThread->pszCmd, rc);
1646
1647 /* Update started/stopped status. */
1648 ASMAtomicXchgBool(&pThread->fStopped, true);
1649 ASMAtomicXchgBool(&pThread->fStarted, false);
1650
1651 /*
1652 * If something went wrong signal the user event so that others don't wait
1653 * forever on this thread.
1654 */
1655 if (RT_FAILURE(rc) && !fSignalled)
1656 RTThreadUserSignal(RTThreadSelf());
1657
1658 return rc;
1659}
1660
1661
1662/**
1663 * Thread main routine for a started process.
1664 *
1665 * @return IPRT status code.
1666 * @param RTTHREAD Pointer to the thread's data.
1667 * @param void* User-supplied argument pointer.
1668 *
1669 */
1670static DECLCALLBACK(int) VBoxServiceControlThread(RTTHREAD ThreadSelf, void *pvUser)
1671{
1672 PVBOXSERVICECTRLTHREAD pThread = (VBOXSERVICECTRLTHREAD*)pvUser;
1673 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
1674 return VBoxServiceControlThreadProcessWorker(pThread);
1675}
1676
1677
1678/**
1679 * Executes (starts) a process on the guest. This causes a new thread to be created
1680 * so that this function will not block the overall program execution.
1681 *
1682 * @return IPRT status code.
1683 * @param uContextID Context ID to associate the process to start with.
1684 * @param pProcess Process info.
1685 */
1686int VBoxServiceControlThreadStart(uint32_t uContextID,
1687 PVBOXSERVICECTRLPROCESS pProcess)
1688{
1689 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1690
1691 /*
1692 * Allocate new thread data and assign it to our thread list.
1693 */
1694 PVBOXSERVICECTRLTHREAD pThread = (PVBOXSERVICECTRLTHREAD)RTMemAlloc(sizeof(VBOXSERVICECTRLTHREAD));
1695 if (!pThread)
1696 return VERR_NO_MEMORY;
1697
1698 int rc = gstsvcCntlExecThreadInit(pThread, pProcess, uContextID);
1699 if (RT_SUCCESS(rc))
1700 {
1701 static uint32_t s_uCtrlExecThread = 0;
1702 if (s_uCtrlExecThread++ == UINT32_MAX)
1703 s_uCtrlExecThread = 0; /* Wrap around to not let IPRT freak out. */
1704 rc = RTThreadCreateF(&pThread->Thread, VBoxServiceControlThread,
1705 pThread /*pvUser*/, 0 /*cbStack*/,
1706 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "gctl%u", s_uCtrlExecThread);
1707 if (RT_FAILURE(rc))
1708 {
1709 VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n, pThread=%p\n",
1710 rc, pThread);
1711 }
1712 else
1713 {
1714 VBoxServiceVerbose(4, "Waiting for thread to initialize ...\n");
1715
1716 /* Wait for the thread to initialize. */
1717 rc = RTThreadUserWait(pThread->Thread, 60 * 1000 /* 60 seconds max. */);
1718 AssertRC(rc);
1719 if ( ASMAtomicReadBool(&pThread->fShutdown)
1720 || RT_FAILURE(rc))
1721 {
1722 VBoxServiceError("Thread for process \"%s\" failed to start, rc=%Rrc\n",
1723 pProcess->szCmd, rc);
1724 }
1725 else
1726 {
1727 ASMAtomicXchgBool(&pThread->fStarted, true);
1728 }
1729 }
1730 }
1731
1732 if (RT_FAILURE(rc))
1733 RTMemFree(pThread);
1734
1735 return rc;
1736}
1737
1738
1739/**
1740 * Performs a request to a specific (formerly started) guest process and waits
1741 * for its response.
1742 *
1743 * @return IPRT status code.
1744 * @param uPID PID of guest process to perform a request to.
1745 * @param pRequest Pointer to request to perform.
1746 */
1747int VBoxServiceControlThreadPerform(uint32_t uPID, PVBOXSERVICECTRLREQUEST pRequest)
1748{
1749 AssertPtrReturn(pRequest, VERR_INVALID_POINTER);
1750 AssertReturn(pRequest->enmType > VBOXSERVICECTRLREQUEST_UNKNOWN, VERR_INVALID_PARAMETER);
1751 /* Rest in pRequest is optional (based on the request type). */
1752
1753 int rc = VINF_SUCCESS;
1754 PVBOXSERVICECTRLTHREAD pThread = VBoxServiceControlLockThread(uPID);
1755 if (pThread)
1756 {
1757 if (ASMAtomicReadBool(&pThread->fShutdown))
1758 {
1759 rc = VERR_CANCELLED;
1760 }
1761 else
1762 {
1763 /* Set request structure pointer. */
1764 pThread->pRequest = pRequest;
1765
1766 /** @todo To speed up simultaneous guest process handling we could add a worker threads
1767 * or queue in order to wait for the request to happen. Later. */
1768 /* Wake up guest thrad by sending a wakeup byte to the notification pipe so
1769 * that RTPoll unblocks (returns) and we then can do our requested operation. */
1770 Assert(pThread->hNotificationPipeW != NIL_RTPIPE);
1771 size_t cbWritten;
1772 if (RT_SUCCESS(rc))
1773 rc = RTPipeWrite(pThread->hNotificationPipeW, "i", 1, &cbWritten);
1774
1775 if ( RT_SUCCESS(rc)
1776 && cbWritten)
1777 {
1778 VBoxServiceVerbose(3, "[PID %u]: Waiting for response on enmType=%u, pvData=0x%p, cbData=%u\n",
1779 uPID, pRequest->enmType, pRequest->pvData, pRequest->cbData);
1780
1781 rc = VBoxServiceControlThreadRequestWait(pRequest);
1782 }
1783 }
1784
1785 VBoxServiceControlUnlockThread(pThread);
1786 }
1787 else /* PID not found! */
1788 rc = VERR_NOT_FOUND;
1789
1790 VBoxServiceVerbose(3, "[PID %u]: Performed enmType=%u, uCID=%u, pvData=0x%p, cbData=%u, rc=%Rrc\n",
1791 uPID, pRequest->enmType, pRequest->uCID, pRequest->pvData, pRequest->cbData, rc);
1792 return rc;
1793}
1794
Note: See TracBrowser for help on using the repository browser.

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