VirtualBox

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

Last change on this file since 44098 was 44046, checked in by vboxsync, 12 years ago

VBoxServiceControlThreadRequestAllocEx: Corrected parameter name and drop unnecessary cast. Looks like someone changed pbData to void * and didn't bother cleaning up.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 66.1 KB
Line 
1/* $Id: VBoxServiceControlThread.cpp 44046 2012-12-06 08:20:58Z 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 default:
650 AssertMsgFailed(("Unknown idPollHnd=%RU32\n", idPollHnd));
651 break;
652 }
653
654 if (RT_FAILURE(rc) || rc == VINF_EOF)
655 break; /* Abort command, or client dead or something. */
656
657 if (RT_UNLIKELY(pThread->fShutdown))
658 break; /* We were asked to shutdown. */
659
660 continue;
661 }
662
663#if 0
664 VBoxServiceVerbose(4, "[PID %u]: Polling done, pollRC=%Rrc, pollCnt=%u, rc=%Rrc, fShutdown=%RTbool\n",
665 pThread->uPID, rc2, RTPollSetGetCount(hPollSet), rc, pThread->fShutdown);
666#endif
667 /*
668 * Check for process death.
669 */
670 if (fProcessAlive)
671 {
672 rc2 = RTProcWaitNoResume(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
673 if (RT_SUCCESS_NP(rc2))
674 {
675 fProcessAlive = false;
676 continue;
677 }
678 if (RT_UNLIKELY(rc2 == VERR_INTERRUPTED))
679 continue;
680 if (RT_UNLIKELY(rc2 == VERR_PROCESS_NOT_FOUND))
681 {
682 fProcessAlive = false;
683 ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
684 ProcessStatus.iStatus = 255;
685 AssertFailed();
686 }
687 else
688 AssertMsg(rc2 == VERR_PROCESS_RUNNING, ("%Rrc\n", rc2));
689 }
690
691 /*
692 * If the process has terminated and all output has been consumed,
693 * we should be heading out.
694 */
695 if ( !fProcessAlive
696 && *phStdOutR == NIL_RTPIPE
697 && *phStdErrR == NIL_RTPIPE)
698 break;
699
700 /*
701 * Check for timed out, killing the process.
702 */
703 uint32_t cMilliesLeft = RT_INDEFINITE_WAIT;
704 if (cMsTimeout != RT_INDEFINITE_WAIT)
705 {
706 uint64_t u64Now = RTTimeMilliTS();
707 uint64_t cMsElapsed = u64Now - MsStart;
708 if (cMsElapsed >= cMsTimeout)
709 {
710 VBoxServiceVerbose(3, "[PID %u]: Timed out (%ums elapsed > %ums timeout), killing ...",
711 pThread->uPID, cMsElapsed, cMsTimeout);
712
713 fProcessTimedOut = true;
714 if ( MsProcessKilled == UINT64_MAX
715 || u64Now - MsProcessKilled > 1000)
716 {
717 if (u64Now - MsProcessKilled > 20*60*1000)
718 break; /* Give up after 20 mins. */
719 RTProcTerminate(hProcess);
720 MsProcessKilled = u64Now;
721 continue;
722 }
723 cMilliesLeft = 10000;
724 }
725 else
726 cMilliesLeft = cMsTimeout - (uint32_t)cMsElapsed;
727 }
728
729 /* Reset the polling interval since we've done all pending work. */
730 cMsPollCur = fProcessAlive
731 ? cMsPollBase
732 : RT_MS_1MIN;
733 if (cMilliesLeft < cMsPollCur)
734 cMsPollCur = cMilliesLeft;
735
736 /*
737 * Need to exit?
738 */
739 if (pThread->fShutdown)
740 break;
741 }
742
743 rc2 = RTCritSectEnter(&pThread->CritSect);
744 if (RT_SUCCESS(rc2))
745 {
746 ASMAtomicXchgBool(&pThread->fShutdown, true);
747
748 rc2 = RTCritSectLeave(&pThread->CritSect);
749 AssertRC(rc2);
750 }
751
752 /*
753 * Try kill the process if it's still alive at this point.
754 */
755 if (fProcessAlive)
756 {
757 if (MsProcessKilled == UINT64_MAX)
758 {
759 VBoxServiceVerbose(3, "[PID %u]: Is still alive and not killed yet\n",
760 pThread->uPID);
761
762 MsProcessKilled = RTTimeMilliTS();
763 RTProcTerminate(hProcess);
764 RTThreadSleep(500);
765 }
766
767 for (size_t i = 0; i < 10; i++)
768 {
769 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Waiting to exit ...\n",
770 pThread->uPID, i + 1);
771 rc2 = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
772 if (RT_SUCCESS(rc2))
773 {
774 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Exited\n",
775 pThread->uPID, i + 1);
776 fProcessAlive = false;
777 break;
778 }
779 if (i >= 5)
780 {
781 VBoxServiceVerbose(4, "[PID %u]: Kill attempt %d/10: Trying to terminate ...\n",
782 pThread->uPID, i + 1);
783 RTProcTerminate(hProcess);
784 }
785 RTThreadSleep(i >= 5 ? 2000 : 500);
786 }
787
788 if (fProcessAlive)
789 VBoxServiceVerbose(3, "[PID %u]: Could not be killed\n", pThread->uPID);
790 }
791
792 /*
793 * If we don't have a client problem (RT_FAILURE(rc)) we'll reply to the
794 * clients exec packet now.
795 */
796 if (RT_SUCCESS(rc))
797 {
798 uint32_t uStatus = PROC_STS_UNDEFINED;
799 uint32_t uFlags = 0;
800
801 if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX)
802 {
803 VBoxServiceVerbose(3, "[PID %u]: Timed out and got killed\n",
804 pThread->uPID);
805 uStatus = PROC_STS_TOK;
806 }
807 else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX)
808 {
809 VBoxServiceVerbose(3, "[PID %u]: Timed out and did *not* get killed\n",
810 pThread->uPID);
811 uStatus = PROC_STS_TOA;
812 }
813 else if (pThread->fShutdown && (fProcessAlive || MsProcessKilled != UINT64_MAX))
814 {
815 VBoxServiceVerbose(3, "[PID %u]: Got terminated because system/service is about to shutdown\n",
816 pThread->uPID);
817 uStatus = PROC_STS_DWN; /* Service is stopping, process was killed. */
818 uFlags = pThread->uFlags; /* Return handed-in execution flags back to the host. */
819 }
820 else if (fProcessAlive)
821 {
822 VBoxServiceError("[PID %u]: Is alive when it should not!\n",
823 pThread->uPID);
824 }
825 else if (MsProcessKilled != UINT64_MAX)
826 {
827 VBoxServiceError("[PID %u]: Has been killed when it should not!\n",
828 pThread->uPID);
829 }
830 else if (ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL)
831 {
832 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_NORMAL (Exit code: %u)\n",
833 pThread->uPID, ProcessStatus.iStatus);
834
835 uStatus = PROC_STS_TEN;
836 uFlags = ProcessStatus.iStatus;
837 }
838 else if (ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL)
839 {
840 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_SIGNAL (Signal: %u)\n",
841 pThread->uPID, ProcessStatus.iStatus);
842
843 uStatus = PROC_STS_TES;
844 uFlags = ProcessStatus.iStatus;
845 }
846 else if (ProcessStatus.enmReason == RTPROCEXITREASON_ABEND)
847 {
848 /* ProcessStatus.iStatus will be undefined. */
849 VBoxServiceVerbose(3, "[PID %u]: Ended with RTPROCEXITREASON_ABEND\n",
850 pThread->uPID);
851
852 uStatus = PROC_STS_TEA;
853 uFlags = ProcessStatus.iStatus;
854 }
855 else
856 VBoxServiceVerbose(1, "[PID %u]: Handling process status %u not implemented\n",
857 pThread->uPID, ProcessStatus.enmReason);
858
859 VBoxServiceVerbose(2, "[PID %u]: Ended, ClientID=%u, CID=%u, Status=%u, Flags=0x%x\n",
860 pThread->uPID, pThread->uClientID, pThread->uContextID, uStatus, uFlags);
861
862 if (!(pThread->uFlags & EXECUTEPROCESSFLAG_WAIT_START))
863 {
864 rc2 = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID,
865 pThread->uPID, uStatus, uFlags,
866 NULL /* pvData */, 0 /* cbData */);
867 if (RT_FAILURE(rc2))
868 VBoxServiceError("[PID %u]: Error reporting final status to host; rc=%Rrc\n",
869 pThread->uPID, rc2);
870 if (RT_SUCCESS(rc))
871 rc = rc2;
872 }
873 else
874 VBoxServiceVerbose(3, "[PID %u]: Was started detached, no final status sent to host\n",
875 pThread->uPID);
876
877 VBoxServiceVerbose(3, "[PID %u]: Process loop ended with rc=%Rrc\n",
878 pThread->uPID, rc);
879 }
880 else
881 VBoxServiceError("[PID %u]: Loop failed with rc=%Rrc\n",
882 pThread->uPID, rc);
883 return rc;
884}
885
886
887/**
888 * Initializes a pipe's handle and pipe object.
889 *
890 * @return IPRT status code.
891 * @param ph The pipe's handle to initialize.
892 * @param phPipe The pipe's object to initialize.
893 */
894static int vboxServiceControlThreadInitPipe(PRTHANDLE ph, PRTPIPE phPipe)
895{
896 AssertPtrReturn(ph, VERR_INVALID_PARAMETER);
897 AssertPtrReturn(phPipe, VERR_INVALID_PARAMETER);
898
899 ph->enmType = RTHANDLETYPE_PIPE;
900 ph->u.hPipe = NIL_RTPIPE;
901 *phPipe = NIL_RTPIPE;
902
903 return VINF_SUCCESS;
904}
905
906
907/**
908 * Allocates a guest thread request with the specified request data.
909 *
910 * @return IPRT status code.
911 * @param ppReq Pointer that will receive the newly allocated request.
912 * Must be freed later with VBoxServiceControlThreadRequestFree().
913 * @param enmType Request type.
914 * @param pvData Payload data, based on request type.
915 * @param cbData Size of payload data (in bytes).
916 * @param uCID Context ID to which this request belongs to.
917 */
918int VBoxServiceControlThreadRequestAllocEx(PVBOXSERVICECTRLREQUEST *ppReq,
919 VBOXSERVICECTRLREQUESTTYPE enmType,
920 void *pvData,
921 size_t cbData,
922 uint32_t uCID)
923{
924 AssertPtrReturn(ppReq, VERR_INVALID_POINTER);
925
926 PVBOXSERVICECTRLREQUEST pReq = (PVBOXSERVICECTRLREQUEST)RTMemAlloc(sizeof(VBOXSERVICECTRLREQUEST));
927 AssertPtrReturn(pReq, VERR_NO_MEMORY);
928
929 RT_ZERO(*pReq);
930 pReq->enmType = enmType;
931 pReq->uCID = uCID;
932 pReq->cbData = cbData;
933 pReq->pvData = pvData;
934
935 /* Set request result to some defined state in case
936 * it got cancelled. */
937 pReq->rc = VERR_CANCELLED;
938
939 int rc = RTSemEventMultiCreate(&pReq->Event);
940 AssertRC(rc);
941
942 if (RT_SUCCESS(rc))
943 {
944 *ppReq = pReq;
945 return VINF_SUCCESS;
946 }
947
948 RTMemFree(pReq);
949 return rc;
950}
951
952
953/**
954 * Allocates a guest thread request with the specified request data.
955 *
956 * @return IPRT status code.
957 * @param ppReq Pointer that will receive the newly allocated request.
958 * Must be freed later with VBoxServiceControlThreadRequestFree().
959 * @param enmType Request type.
960 */
961int VBoxServiceControlThreadRequestAlloc(PVBOXSERVICECTRLREQUEST *ppReq,
962 VBOXSERVICECTRLREQUESTTYPE enmType)
963{
964 return VBoxServiceControlThreadRequestAllocEx(ppReq, enmType,
965 NULL /* pvData */, 0 /* cbData */,
966 0 /* ContextID */);
967}
968
969
970/**
971 * Cancels a previously fired off guest thread request.
972 *
973 * Note: Does *not* do locking since VBoxServiceControlThreadRequestWait()
974 * holds the lock (critsect); so only trigger the signal; the owner
975 * needs to clean up afterwards.
976 *
977 * @return IPRT status code.
978 * @param pReq Request to cancel.
979 */
980static int vboxServiceControlThreadRequestCancel(PVBOXSERVICECTRLREQUEST pReq)
981{
982 if (!pReq) /* Silently skip non-initialized requests. */
983 return VINF_SUCCESS;
984
985 VBoxServiceVerbose(4, "Cancelling request=0x%p\n", pReq);
986
987 return RTSemEventMultiSignal(pReq->Event);
988}
989
990
991/**
992 * Frees a formerly allocated guest thread request.
993 *
994 * @return IPRT status code.
995 * @param pReq Request to free.
996 */
997void VBoxServiceControlThreadRequestFree(PVBOXSERVICECTRLREQUEST pReq)
998{
999 AssertPtrReturnVoid(pReq);
1000
1001 VBoxServiceVerbose(4, "Freeing request=0x%p (event=%RTsem)\n",
1002 pReq, &pReq->Event);
1003
1004 int rc = RTSemEventMultiDestroy(pReq->Event);
1005 AssertRC(rc);
1006
1007 RTMemFree(pReq);
1008 pReq = NULL;
1009}
1010
1011
1012/**
1013 * Waits for a guest thread's event to get triggered.
1014 *
1015 * @return IPRT status code.
1016 * @param pReq Request to wait for.
1017 */
1018int VBoxServiceControlThreadRequestWait(PVBOXSERVICECTRLREQUEST pReq)
1019{
1020 AssertPtrReturn(pReq, VERR_INVALID_POINTER);
1021
1022 /* Wait on the request to get completed (or we are asked to abort/shutdown). */
1023 int rc = RTSemEventMultiWait(pReq->Event, RT_INDEFINITE_WAIT);
1024 if (RT_SUCCESS(rc))
1025 {
1026 VBoxServiceVerbose(4, "Performed request with rc=%Rrc, cbData=%u\n",
1027 pReq->rc, pReq->cbData);
1028
1029 /* Give back overall request result. */
1030 rc = pReq->rc;
1031 }
1032 else
1033 VBoxServiceError("Waiting for request failed, rc=%Rrc\n", rc);
1034
1035 return rc;
1036}
1037
1038
1039/**
1040 * Sets up the redirection / pipe / nothing for one of the standard handles.
1041 *
1042 * @returns IPRT status code. No client replies made.
1043 * @param pszHowTo How to set up this standard handle.
1044 * @param fd Which standard handle it is (0 == stdin, 1 ==
1045 * stdout, 2 == stderr).
1046 * @param ph The generic handle that @a pph may be set
1047 * pointing to. Always set.
1048 * @param pph Pointer to the RTProcCreateExec argument.
1049 * Always set.
1050 * @param phPipe Where to return the end of the pipe that we
1051 * should service.
1052 */
1053static int VBoxServiceControlThreadSetupPipe(const char *pszHowTo, int fd,
1054 PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe)
1055{
1056 AssertPtrReturn(ph, VERR_INVALID_POINTER);
1057 AssertPtrReturn(pph, VERR_INVALID_POINTER);
1058 AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
1059
1060 int rc;
1061
1062 ph->enmType = RTHANDLETYPE_PIPE;
1063 ph->u.hPipe = NIL_RTPIPE;
1064 *pph = NULL;
1065 *phPipe = NIL_RTPIPE;
1066
1067 if (!strcmp(pszHowTo, "|"))
1068 {
1069 /*
1070 * Setup a pipe for forwarding to/from the client.
1071 * The ph union struct will be filled with a pipe read/write handle
1072 * to represent the "other" end to phPipe.
1073 */
1074 if (fd == 0) /* stdin? */
1075 {
1076 /* Connect a wrtie pipe specified by phPipe to stdin. */
1077 rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ);
1078 }
1079 else /* stdout or stderr? */
1080 {
1081 /* Connect a read pipe specified by phPipe to stdout or stderr. */
1082 rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE);
1083 }
1084
1085 if (RT_FAILURE(rc))
1086 return rc;
1087
1088 ph->enmType = RTHANDLETYPE_PIPE;
1089 *pph = ph;
1090 }
1091 else if (!strcmp(pszHowTo, "/dev/null"))
1092 {
1093 /*
1094 * Redirect to/from /dev/null.
1095 */
1096 RTFILE hFile;
1097 rc = RTFileOpenBitBucket(&hFile, fd == 0 ? RTFILE_O_READ : RTFILE_O_WRITE);
1098 if (RT_FAILURE(rc))
1099 return rc;
1100
1101 ph->enmType = RTHANDLETYPE_FILE;
1102 ph->u.hFile = hFile;
1103 *pph = ph;
1104 }
1105 else /* Add other piping stuff here. */
1106 rc = VINF_SUCCESS; /* Same as parent (us). */
1107
1108 return rc;
1109}
1110
1111
1112/**
1113 * Expands a file name / path to its real content. This only works on Windows
1114 * for now (e.g. translating "%TEMP%\foo.exe" to "C:\Windows\Temp" when starting
1115 * with system / administrative rights).
1116 *
1117 * @return IPRT status code.
1118 * @param pszPath Path to resolve.
1119 * @param pszExpanded Pointer to string to store the resolved path in.
1120 * @param cbExpanded Size (in bytes) of string to store the resolved path.
1121 */
1122static int VBoxServiceControlThreadMakeFullPath(const char *pszPath, char *pszExpanded, size_t cbExpanded)
1123{
1124 int rc = VINF_SUCCESS;
1125#ifdef RT_OS_WINDOWS
1126 if (!ExpandEnvironmentStrings(pszPath, pszExpanded, cbExpanded))
1127 rc = RTErrConvertFromWin32(GetLastError());
1128#else
1129 /* No expansion for non-Windows yet. */
1130 rc = RTStrCopy(pszExpanded, cbExpanded, pszPath);
1131#endif
1132#ifdef DEBUG
1133 VBoxServiceVerbose(3, "VBoxServiceControlExecMakeFullPath: %s -> %s\n",
1134 pszPath, pszExpanded);
1135#endif
1136 return rc;
1137}
1138
1139
1140/**
1141 * Resolves the full path of a specified executable name. This function also
1142 * resolves internal VBoxService tools to its appropriate executable path + name if
1143 * VBOXSERVICE_NAME is specified as pszFileName.
1144 *
1145 * @return IPRT status code.
1146 * @param pszFileName File name to resolve.
1147 * @param pszResolved Pointer to a string where the resolved file name will be stored.
1148 * @param cbResolved Size (in bytes) of resolved file name string.
1149 */
1150static int VBoxServiceControlThreadResolveExecutable(const char *pszFileName,
1151 char *pszResolved, size_t cbResolved)
1152{
1153 AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
1154 AssertPtrReturn(pszResolved, VERR_INVALID_POINTER);
1155 AssertReturn(cbResolved, VERR_INVALID_PARAMETER);
1156
1157 int rc = VINF_SUCCESS;
1158
1159 char szPathToResolve[RTPATH_MAX];
1160 if ( (g_pszProgName && (RTStrICmp(pszFileName, g_pszProgName) == 0))
1161 || !RTStrICmp(pszFileName, VBOXSERVICE_NAME))
1162 {
1163 /* Resolve executable name of this process. */
1164 if (!RTProcGetExecutablePath(szPathToResolve, sizeof(szPathToResolve)))
1165 rc = VERR_FILE_NOT_FOUND;
1166 }
1167 else
1168 {
1169 /* Take the raw argument to resolve. */
1170 rc = RTStrCopy(szPathToResolve, sizeof(szPathToResolve), pszFileName);
1171 }
1172
1173 if (RT_SUCCESS(rc))
1174 {
1175 rc = VBoxServiceControlThreadMakeFullPath(szPathToResolve, pszResolved, cbResolved);
1176 if (RT_SUCCESS(rc))
1177 VBoxServiceVerbose(3, "Looked up executable: %s -> %s\n",
1178 pszFileName, pszResolved);
1179 }
1180
1181 if (RT_FAILURE(rc))
1182 VBoxServiceError("Failed to lookup executable \"%s\" with rc=%Rrc\n",
1183 pszFileName, rc);
1184 return rc;
1185}
1186
1187
1188/**
1189 * Constructs the argv command line by resolving environment variables
1190 * and relative paths.
1191 *
1192 * @return IPRT status code.
1193 * @param pszArgv0 First argument (argv0), either original or modified version. Optional.
1194 * @param papszArgs Original argv command line from the host, starting at argv[1].
1195 * @param ppapszArgv Pointer to a pointer with the new argv command line.
1196 * Needs to be freed with RTGetOptArgvFree.
1197 */
1198static int VBoxServiceControlThreadAllocateArgv(const char *pszArgv0,
1199 const char * const *papszArgs,
1200 bool fExpandArgs, char ***ppapszArgv)
1201{
1202 AssertPtrReturn(ppapszArgv, VERR_INVALID_POINTER);
1203
1204 VBoxServiceVerbose(3, "VBoxServiceControlThreadPrepareArgv: pszArgv0=%p, papszArgs=%p, fExpandArgs=%RTbool, ppapszArgv=%p\n",
1205 pszArgv0, papszArgs, fExpandArgs, ppapszArgv);
1206
1207 int rc = VINF_SUCCESS;
1208 uint32_t cArgs;
1209 for (cArgs = 0; papszArgs[cArgs]; cArgs++)
1210 {
1211 if (cArgs >= UINT32_MAX - 2)
1212 return VERR_BUFFER_OVERFLOW;
1213 }
1214
1215 /* Allocate new argv vector (adding + 2 for argv0 + termination). */
1216 size_t cbSize = (cArgs + 2) * sizeof(char*);
1217 char **papszNewArgv = (char**)RTMemAlloc(cbSize);
1218 if (!papszNewArgv)
1219 return VERR_NO_MEMORY;
1220
1221#ifdef DEBUG
1222 VBoxServiceVerbose(3, "VBoxServiceControlThreadPrepareArgv: cbSize=%RU32, cArgs=%RU32\n",
1223 cbSize, cArgs);
1224#endif
1225
1226 size_t i = 0; /* Keep the argument counter in scope for cleaning up on failure. */
1227
1228 rc = RTStrDupEx(&papszNewArgv[0], pszArgv0);
1229 if (RT_SUCCESS(rc))
1230 {
1231 for (; i < cArgs; i++)
1232 {
1233 char *pszArg;
1234#if 0 /* Arguments expansion -- untested. */
1235 if (fExpandArgs)
1236 {
1237 /* According to MSDN the limit on older Windows version is 32K, whereas
1238 * Vista+ there are no limits anymore. We still stick to 4K. */
1239 char szExpanded[_4K];
1240# ifdef RT_OS_WINDOWS
1241 if (!ExpandEnvironmentStrings(papszArgs[i], szExpanded, sizeof(szExpanded)))
1242 rc = RTErrConvertFromWin32(GetLastError());
1243# else
1244 /* No expansion for non-Windows yet. */
1245 rc = RTStrCopy(papszArgs[i], sizeof(szExpanded), szExpanded);
1246# endif
1247 if (RT_SUCCESS(rc))
1248 rc = RTStrDupEx(&pszArg, szExpanded);
1249 }
1250 else
1251#endif
1252 rc = RTStrDupEx(&pszArg, papszArgs[i]);
1253
1254 if (RT_FAILURE(rc))
1255 break;
1256
1257 papszNewArgv[i + 1] = pszArg;
1258 }
1259
1260 if (RT_SUCCESS(rc))
1261 {
1262 /* Terminate array. */
1263 papszNewArgv[cArgs + 1] = NULL;
1264
1265 *ppapszArgv = papszNewArgv;
1266 }
1267 }
1268
1269 if (RT_FAILURE(rc))
1270 {
1271 for (i; i > 0; i--)
1272 RTStrFree(papszNewArgv[i]);
1273 RTMemFree(papszNewArgv);
1274 }
1275
1276 return rc;
1277}
1278
1279
1280void VBoxServiceControlThreadFreeArgv(char **papszArgv)
1281{
1282 if (papszArgv)
1283 {
1284 size_t i = 0;
1285 while (papszArgv[i])
1286 RTStrFree(papszArgv[i++]);
1287 RTMemFree(papszArgv);
1288 }
1289}
1290
1291
1292/**
1293 * Helper function to create/start a process on the guest.
1294 *
1295 * @return IPRT status code.
1296 * @param pszExec Full qualified path of process to start (without arguments).
1297 * @param papszArgs Pointer to array of command line arguments.
1298 * @param hEnv Handle to environment block to use.
1299 * @param fFlags Process execution flags.
1300 * @param phStdIn Handle for the process' stdin pipe.
1301 * @param phStdOut Handle for the process' stdout pipe.
1302 * @param phStdErr Handle for the process' stderr pipe.
1303 * @param pszAsUser User name (account) to start the process under.
1304 * @param pszPassword Password of the specified user.
1305 * @param phProcess Pointer which will receive the process handle after
1306 * successful process start.
1307 */
1308static int VBoxServiceControlThreadCreateProcess(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
1309 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
1310 const char *pszPassword, PRTPROCESS phProcess)
1311{
1312 AssertPtrReturn(pszExec, VERR_INVALID_PARAMETER);
1313 AssertPtrReturn(papszArgs, VERR_INVALID_PARAMETER);
1314 AssertPtrReturn(phProcess, VERR_INVALID_PARAMETER);
1315
1316 int rc = VINF_SUCCESS;
1317 char szExecExp[RTPATH_MAX];
1318
1319 /* Do we need to expand environment variables in arguments? */
1320 bool fExpandArgs = (fFlags & EXECUTEPROCESSFLAG_EXPAND_ARGUMENTS) ? true : false;
1321
1322#ifdef RT_OS_WINDOWS
1323 /*
1324 * If sysprep should be executed do this in the context of VBoxService, which
1325 * (usually, if started by SCM) has administrator rights. Because of that a UI
1326 * won't be shown (doesn't have a desktop).
1327 */
1328 if (!RTStrICmp(pszExec, "sysprep"))
1329 {
1330 /* Use a predefined sysprep path as default. */
1331 char szSysprepCmd[RTPATH_MAX] = "C:\\sysprep\\sysprep.exe";
1332
1333 /*
1334 * On Windows Vista (and up) sysprep is located in "system32\\sysprep\\sysprep.exe",
1335 * so detect the OS and use a different path.
1336 */
1337 OSVERSIONINFOEX OSInfoEx;
1338 RT_ZERO(OSInfoEx);
1339 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1340 if ( GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
1341 && OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
1342 && OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
1343 {
1344 rc = RTEnvGetEx(RTENV_DEFAULT, "windir", szSysprepCmd, sizeof(szSysprepCmd), NULL);
1345 if (RT_SUCCESS(rc))
1346 rc = RTPathAppend(szSysprepCmd, sizeof(szSysprepCmd), "system32\\sysprep\\sysprep.exe");
1347 }
1348
1349 if (RT_SUCCESS(rc))
1350 {
1351 char **papszArgsExp;
1352 rc = VBoxServiceControlThreadAllocateArgv(szSysprepCmd /* argv0 */, papszArgs,
1353 fExpandArgs, &papszArgsExp);
1354 if (RT_SUCCESS(rc))
1355 {
1356 rc = RTProcCreateEx(szSysprepCmd, papszArgsExp, hEnv, 0 /* fFlags */,
1357 phStdIn, phStdOut, phStdErr, NULL /* pszAsUser */,
1358 NULL /* pszPassword */, phProcess);
1359 VBoxServiceControlThreadFreeArgv(papszArgsExp);
1360 }
1361 }
1362
1363 if (RT_FAILURE(rc))
1364 VBoxServiceVerbose(3, "Starting sysprep returned rc=%Rrc\n", rc);
1365
1366 return rc;
1367 }
1368#endif /* RT_OS_WINDOWS */
1369
1370#ifdef VBOXSERVICE_TOOLBOX
1371 if (RTStrStr(pszExec, "vbox_") == pszExec)
1372 {
1373 /* We want to use the internal toolbox (all internal
1374 * tools are starting with "vbox_" (e.g. "vbox_cat"). */
1375 rc = VBoxServiceControlThreadResolveExecutable(VBOXSERVICE_NAME, szExecExp, sizeof(szExecExp));
1376 }
1377 else
1378 {
1379#endif
1380 /*
1381 * Do the environment variables expansion on executable and arguments.
1382 */
1383 rc = VBoxServiceControlThreadResolveExecutable(pszExec, szExecExp, sizeof(szExecExp));
1384#ifdef VBOXSERVICE_TOOLBOX
1385 }
1386#endif
1387 if (RT_SUCCESS(rc))
1388 {
1389 char **papszArgsExp;
1390 rc = VBoxServiceControlThreadAllocateArgv(pszExec /* Always use the unmodified executable name as argv0. */,
1391 papszArgs /* Append the rest of the argument vector (if any). */,
1392 fExpandArgs, &papszArgsExp);
1393 if (RT_FAILURE(rc))
1394 {
1395 /* Don't print any arguments -- may contain passwords or other sensible data! */
1396 VBoxServiceError("Could not prepare arguments, rc=%Rrc\n", rc);
1397 }
1398 else
1399 {
1400 uint32_t uProcFlags = 0;
1401 if (fFlags)
1402 {
1403 if (fFlags & EXECUTEPROCESSFLAG_HIDDEN)
1404 uProcFlags |= RTPROC_FLAGS_HIDDEN;
1405 if (fFlags & EXECUTEPROCESSFLAG_NO_PROFILE)
1406 uProcFlags |= RTPROC_FLAGS_NO_PROFILE;
1407 }
1408
1409 /* If no user name specified run with current credentials (e.g.
1410 * full service/system rights). This is prohibited via official Main API!
1411 *
1412 * Otherwise use the RTPROC_FLAGS_SERVICE to use some special authentication
1413 * code (at least on Windows) for running processes as different users
1414 * started from our system service. */
1415 if (*pszAsUser)
1416 uProcFlags |= RTPROC_FLAGS_SERVICE;
1417#ifdef DEBUG
1418 VBoxServiceVerbose(3, "Command: %s\n", szExecExp);
1419 for (size_t i = 0; papszArgsExp[i]; i++)
1420 VBoxServiceVerbose(3, "\targv[%ld]: %s\n", i, papszArgsExp[i]);
1421#endif
1422 VBoxServiceVerbose(3, "Starting process \"%s\" ...\n", szExecExp);
1423
1424 /* Do normal execution. */
1425 rc = RTProcCreateEx(szExecExp, papszArgsExp, hEnv, uProcFlags,
1426 phStdIn, phStdOut, phStdErr,
1427 *pszAsUser ? pszAsUser : NULL,
1428 *pszPassword ? pszPassword : NULL,
1429 phProcess);
1430
1431 VBoxServiceVerbose(3, "Starting process \"%s\" returned rc=%Rrc\n",
1432 szExecExp, rc);
1433
1434 VBoxServiceControlThreadFreeArgv(papszArgsExp);
1435 }
1436 }
1437 return rc;
1438}
1439
1440/**
1441 * The actual worker routine (loop) for a started guest process.
1442 *
1443 * @return IPRT status code.
1444 * @param PVBOXSERVICECTRLTHREAD Thread data associated with a started process.
1445 */
1446static int VBoxServiceControlThreadProcessWorker(PVBOXSERVICECTRLTHREAD pThread)
1447{
1448 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
1449 VBoxServiceVerbose(3, "Thread of process pThread=0x%p = \"%s\" started\n",
1450 pThread, pThread->pszCmd);
1451
1452 int rc = VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLIST_RUNNING, pThread);
1453 AssertRC(rc);
1454
1455 rc = VbglR3GuestCtrlConnect(&pThread->uClientID);
1456 if (RT_FAILURE(rc))
1457 {
1458 VBoxServiceError("Thread failed to connect to the guest control service, aborted! Error: %Rrc\n", rc);
1459 RTThreadUserSignal(RTThreadSelf());
1460 return rc;
1461 }
1462 VBoxServiceVerbose(3, "Guest process \"%s\" got client ID=%u, flags=0x%x\n",
1463 pThread->pszCmd, pThread->uClientID, pThread->uFlags);
1464
1465 bool fSignalled = false; /* Indicator whether we signalled the thread user event already. */
1466
1467 /*
1468 * Create the environment.
1469 */
1470 RTENV hEnv;
1471 rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
1472 if (RT_SUCCESS(rc))
1473 {
1474 size_t i;
1475 for (i = 0; i < pThread->uNumEnvVars && pThread->papszEnv; i++)
1476 {
1477 rc = RTEnvPutEx(hEnv, pThread->papszEnv[i]);
1478 if (RT_FAILURE(rc))
1479 break;
1480 }
1481 if (RT_SUCCESS(rc))
1482 {
1483 /*
1484 * Setup the redirection of the standard stuff.
1485 */
1486 /** @todo consider supporting: gcc stuff.c >file 2>&1. */
1487 RTHANDLE hStdIn;
1488 PRTHANDLE phStdIn;
1489 rc = VBoxServiceControlThreadSetupPipe("|", 0 /*STDIN_FILENO*/,
1490 &hStdIn, &phStdIn, &pThread->pipeStdInW);
1491 if (RT_SUCCESS(rc))
1492 {
1493 RTHANDLE hStdOut;
1494 PRTHANDLE phStdOut;
1495 RTPIPE pipeStdOutR;
1496 rc = VBoxServiceControlThreadSetupPipe( (pThread->uFlags & EXECUTEPROCESSFLAG_WAIT_STDOUT)
1497 ? "|" : "/dev/null",
1498 1 /*STDOUT_FILENO*/,
1499 &hStdOut, &phStdOut, &pipeStdOutR);
1500 if (RT_SUCCESS(rc))
1501 {
1502 RTHANDLE hStdErr;
1503 PRTHANDLE phStdErr;
1504 RTPIPE pipeStdErrR;
1505 rc = VBoxServiceControlThreadSetupPipe( (pThread->uFlags & EXECUTEPROCESSFLAG_WAIT_STDERR)
1506 ? "|" : "/dev/null",
1507 2 /*STDERR_FILENO*/,
1508 &hStdErr, &phStdErr, &pipeStdErrR);
1509 if (RT_SUCCESS(rc))
1510 {
1511 /*
1512 * Create a poll set for the pipes and let the
1513 * transport layer add stuff to it as well.
1514 */
1515 RTPOLLSET hPollSet;
1516 rc = RTPollSetCreate(&hPollSet);
1517 if (RT_SUCCESS(rc))
1518 {
1519 uint32_t uFlags = RTPOLL_EVT_ERROR;
1520#if 0
1521 /* Add reading event to pollset to get some more information. */
1522 uFlags |= RTPOLL_EVT_READ;
1523#endif
1524 /* Stdin. */
1525 if (RT_SUCCESS(rc))
1526 rc = RTPollSetAddPipe(hPollSet, pThread->pipeStdInW, RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDIN);
1527 /* Stdout. */
1528 if (RT_SUCCESS(rc))
1529 rc = RTPollSetAddPipe(hPollSet, pipeStdOutR, uFlags, VBOXSERVICECTRLPIPEID_STDOUT);
1530 /* Stderr. */
1531 if (RT_SUCCESS(rc))
1532 rc = RTPollSetAddPipe(hPollSet, pipeStdErrR, uFlags, VBOXSERVICECTRLPIPEID_STDERR);
1533 /* IPC notification pipe. */
1534 if (RT_SUCCESS(rc))
1535 rc = RTPipeCreate(&pThread->hNotificationPipeR, &pThread->hNotificationPipeW, 0 /* Flags */);
1536 if (RT_SUCCESS(rc))
1537 rc = RTPollSetAddPipe(hPollSet, pThread->hNotificationPipeR, RTPOLL_EVT_READ, VBOXSERVICECTRLPIPEID_IPC_NOTIFY);
1538
1539 if (RT_SUCCESS(rc))
1540 {
1541 RTPROCESS hProcess;
1542 rc = VBoxServiceControlThreadCreateProcess(pThread->pszCmd, pThread->papszArgs, hEnv, pThread->uFlags,
1543 phStdIn, phStdOut, phStdErr,
1544 pThread->pszUser, pThread->pszPassword,
1545 &hProcess);
1546 if (RT_FAILURE(rc))
1547 VBoxServiceError("Error starting process, rc=%Rrc\n", rc);
1548 /*
1549 * Tell the control thread that it can continue
1550 * spawning services. This needs to be done after the new
1551 * process has been started because otherwise signal handling
1552 * on (Open) Solaris does not work correctly (see @bugref{5068}).
1553 */
1554 int rc2 = RTThreadUserSignal(RTThreadSelf());
1555 if (RT_SUCCESS(rc))
1556 rc = rc2;
1557 fSignalled = true;
1558
1559 if (RT_SUCCESS(rc))
1560 {
1561 /*
1562 * Close the child ends of any pipes and redirected files.
1563 */
1564 rc2 = RTHandleClose(phStdIn); AssertRC(rc2);
1565 phStdIn = NULL;
1566 rc2 = RTHandleClose(phStdOut); AssertRC(rc2);
1567 phStdOut = NULL;
1568 rc2 = RTHandleClose(phStdErr); AssertRC(rc2);
1569 phStdErr = NULL;
1570
1571 /* Enter the process loop. */
1572 rc = VBoxServiceControlThreadProcLoop(pThread,
1573 hProcess, pThread->uTimeLimitMS, hPollSet,
1574 &pThread->pipeStdInW, &pipeStdOutR, &pipeStdErrR);
1575
1576 /*
1577 * The handles that are no longer in the set have
1578 * been closed by the above call in order to prevent
1579 * the guest from getting stuck accessing them.
1580 * So, NIL the handles to avoid closing them again.
1581 */
1582 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_IPC_NOTIFY, NULL)))
1583 {
1584 pThread->hNotificationPipeR = NIL_RTPIPE;
1585 pThread->hNotificationPipeW = NIL_RTPIPE;
1586 }
1587 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDERR, NULL)))
1588 pipeStdErrR = NIL_RTPIPE;
1589 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDOUT, NULL)))
1590 pipeStdOutR = NIL_RTPIPE;
1591 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDIN, NULL)))
1592 pThread->pipeStdInW = NIL_RTPIPE;
1593 }
1594 }
1595 RTPollSetDestroy(hPollSet);
1596
1597 RTPipeClose(pThread->hNotificationPipeR);
1598 pThread->hNotificationPipeR = NIL_RTPIPE;
1599 RTPipeClose(pThread->hNotificationPipeW);
1600 pThread->hNotificationPipeW = NIL_RTPIPE;
1601 }
1602 RTPipeClose(pipeStdErrR);
1603 pipeStdErrR = NIL_RTPIPE;
1604 RTHandleClose(phStdErr);
1605 if (phStdErr)
1606 RTHandleClose(phStdErr);
1607 }
1608 RTPipeClose(pipeStdOutR);
1609 pipeStdOutR = NIL_RTPIPE;
1610 RTHandleClose(&hStdOut);
1611 if (phStdOut)
1612 RTHandleClose(phStdOut);
1613 }
1614 RTPipeClose(pThread->pipeStdInW);
1615 pThread->pipeStdInW = NIL_RTPIPE;
1616 RTHandleClose(phStdIn);
1617 }
1618 }
1619 RTEnvDestroy(hEnv);
1620 }
1621
1622 /* Move thread to stopped thread list. */
1623 int rc2 = VBoxServiceControlListSet(VBOXSERVICECTRLTHREADLIST_STOPPED, pThread);
1624 AssertRC(rc2);
1625
1626 if (pThread->uClientID)
1627 {
1628 if (RT_FAILURE(rc))
1629 {
1630 rc2 = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID, pThread->uPID,
1631 PROC_STS_ERROR, rc,
1632 NULL /* pvData */, 0 /* cbData */);
1633 if (RT_FAILURE(rc2))
1634 VBoxServiceError("Could not report process failure error; rc=%Rrc (process error %Rrc)\n",
1635 rc2, rc);
1636 }
1637
1638 VBoxServiceVerbose(3, "[PID %u]: Cancelling pending host requests (client ID=%u)\n",
1639 pThread->uPID, pThread->uClientID);
1640 rc2 = VbglR3GuestCtrlCancelPendingWaits(pThread->uClientID);
1641 if (RT_FAILURE(rc2))
1642 {
1643 VBoxServiceError("[PID %u]: Cancelling pending host requests failed; rc=%Rrc\n",
1644 pThread->uPID, rc2);
1645 if (RT_SUCCESS(rc))
1646 rc = rc2;
1647 }
1648
1649 /* Disconnect from guest control service. */
1650 VBoxServiceVerbose(3, "[PID %u]: Disconnecting (client ID=%u) ...\n",
1651 pThread->uPID, pThread->uClientID);
1652 VbglR3GuestCtrlDisconnect(pThread->uClientID);
1653 pThread->uClientID = 0;
1654 }
1655
1656 VBoxServiceVerbose(3, "[PID %u]: Thread of process \"%s\" ended with rc=%Rrc\n",
1657 pThread->uPID, pThread->pszCmd, rc);
1658
1659 /* Update started/stopped status. */
1660 ASMAtomicXchgBool(&pThread->fStopped, true);
1661 ASMAtomicXchgBool(&pThread->fStarted, false);
1662
1663 /*
1664 * If something went wrong signal the user event so that others don't wait
1665 * forever on this thread.
1666 */
1667 if (RT_FAILURE(rc) && !fSignalled)
1668 RTThreadUserSignal(RTThreadSelf());
1669
1670 return rc;
1671}
1672
1673
1674/**
1675 * Thread main routine for a started process.
1676 *
1677 * @return IPRT status code.
1678 * @param RTTHREAD Pointer to the thread's data.
1679 * @param void* User-supplied argument pointer.
1680 *
1681 */
1682static DECLCALLBACK(int) VBoxServiceControlThread(RTTHREAD ThreadSelf, void *pvUser)
1683{
1684 PVBOXSERVICECTRLTHREAD pThread = (VBOXSERVICECTRLTHREAD*)pvUser;
1685 AssertPtrReturn(pThread, VERR_INVALID_POINTER);
1686 return VBoxServiceControlThreadProcessWorker(pThread);
1687}
1688
1689
1690/**
1691 * Executes (starts) a process on the guest. This causes a new thread to be created
1692 * so that this function will not block the overall program execution.
1693 *
1694 * @return IPRT status code.
1695 * @param uContextID Context ID to associate the process to start with.
1696 * @param pProcess Process info.
1697 */
1698int VBoxServiceControlThreadStart(uint32_t uContextID,
1699 PVBOXSERVICECTRLPROCESS pProcess)
1700{
1701 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1702
1703 /*
1704 * Allocate new thread data and assign it to our thread list.
1705 */
1706 PVBOXSERVICECTRLTHREAD pThread = (PVBOXSERVICECTRLTHREAD)RTMemAlloc(sizeof(VBOXSERVICECTRLTHREAD));
1707 if (!pThread)
1708 return VERR_NO_MEMORY;
1709
1710 int rc = gstsvcCntlExecThreadInit(pThread, pProcess, uContextID);
1711 if (RT_SUCCESS(rc))
1712 {
1713 static uint32_t s_uCtrlExecThread = 0;
1714 if (s_uCtrlExecThread++ == UINT32_MAX)
1715 s_uCtrlExecThread = 0; /* Wrap around to not let IPRT freak out. */
1716 rc = RTThreadCreateF(&pThread->Thread, VBoxServiceControlThread,
1717 pThread /*pvUser*/, 0 /*cbStack*/,
1718 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "gctl%u", s_uCtrlExecThread);
1719 if (RT_FAILURE(rc))
1720 {
1721 VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n, pThread=%p\n",
1722 rc, pThread);
1723 }
1724 else
1725 {
1726 VBoxServiceVerbose(4, "Waiting for thread to initialize ...\n");
1727
1728 /* Wait for the thread to initialize. */
1729 rc = RTThreadUserWait(pThread->Thread, 60 * 1000 /* 60 seconds max. */);
1730 AssertRC(rc);
1731 if ( ASMAtomicReadBool(&pThread->fShutdown)
1732 || RT_FAILURE(rc))
1733 {
1734 VBoxServiceError("Thread for process \"%s\" failed to start, rc=%Rrc\n",
1735 pProcess->szCmd, rc);
1736 }
1737 else
1738 {
1739 ASMAtomicXchgBool(&pThread->fStarted, true);
1740 }
1741 }
1742 }
1743
1744 if (RT_FAILURE(rc))
1745 RTMemFree(pThread);
1746
1747 return rc;
1748}
1749
1750
1751/**
1752 * Performs a request to a specific (formerly started) guest process and waits
1753 * for its response.
1754 *
1755 * @return IPRT status code.
1756 * @param uPID PID of guest process to perform a request to.
1757 * @param pRequest Pointer to request to perform.
1758 */
1759int VBoxServiceControlThreadPerform(uint32_t uPID, PVBOXSERVICECTRLREQUEST pRequest)
1760{
1761 AssertPtrReturn(pRequest, VERR_INVALID_POINTER);
1762 AssertReturn(pRequest->enmType > VBOXSERVICECTRLREQUEST_UNKNOWN, VERR_INVALID_PARAMETER);
1763 /* Rest in pRequest is optional (based on the request type). */
1764
1765 int rc = VINF_SUCCESS;
1766 PVBOXSERVICECTRLTHREAD pThread = VBoxServiceControlLockThread(uPID);
1767 if (pThread)
1768 {
1769 if (ASMAtomicReadBool(&pThread->fShutdown))
1770 {
1771 rc = VERR_CANCELLED;
1772 }
1773 else
1774 {
1775 /* Set request structure pointer. */
1776 pThread->pRequest = pRequest;
1777
1778 /** @todo To speed up simultaneous guest process handling we could add a worker threads
1779 * or queue in order to wait for the request to happen. Later. */
1780 /* Wake up guest thrad by sending a wakeup byte to the notification pipe so
1781 * that RTPoll unblocks (returns) and we then can do our requested operation. */
1782 Assert(pThread->hNotificationPipeW != NIL_RTPIPE);
1783 size_t cbWritten;
1784 if (RT_SUCCESS(rc))
1785 rc = RTPipeWrite(pThread->hNotificationPipeW, "i", 1, &cbWritten);
1786
1787 if ( RT_SUCCESS(rc)
1788 && cbWritten)
1789 {
1790 VBoxServiceVerbose(3, "[PID %u]: Waiting for response on enmType=%u, pvData=0x%p, cbData=%u\n",
1791 uPID, pRequest->enmType, pRequest->pvData, pRequest->cbData);
1792
1793 rc = VBoxServiceControlThreadRequestWait(pRequest);
1794 }
1795 }
1796
1797 VBoxServiceControlUnlockThread(pThread);
1798 }
1799 else /* PID not found! */
1800 rc = VERR_NOT_FOUND;
1801
1802 VBoxServiceVerbose(3, "[PID %u]: Performed enmType=%u, uCID=%u, pvData=0x%p, cbData=%u, rc=%Rrc\n",
1803 uPID, pRequest->enmType, pRequest->uCID, pRequest->pvData, pRequest->cbData, rc);
1804 return rc;
1805}
1806
Note: See TracBrowser for help on using the repository browser.

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