VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlProcess.cpp@ 51205

Last change on this file since 51205 was 51205, checked in by vboxsync, 11 years ago

Additions/VBoxService: when starting guest processes, ignore VERR_INTERRUPTED when reporting the status to the host because SIGCHLD might be triggered by a very fast child which already terminated at this point

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 80.1 KB
Line 
1/* $Id: VBoxServiceControlProcess.cpp 51205 2014-05-07 14:19:19Z vboxsync $ */
2/** @file
3 * VBoxServiceControlThread - Guest process handling.
4 */
5
6/*
7 * Copyright (C) 2012-2013 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#include "VBoxServiceControl.h"
42
43using namespace guestControl;
44
45/*******************************************************************************
46* Internal Functions *
47*******************************************************************************/
48static int gstcntlProcessAssignPID(PVBOXSERVICECTRLPROCESS pThread, uint32_t uPID);
49static int gstcntlProcessLock(PVBOXSERVICECTRLPROCESS pProcess);
50static int gstcntlProcessRequest(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, PFNRT pfnFunction, unsigned cArgs, ...);
51static int gstcntlProcessSetupPipe(const char *pszHowTo, int fd, PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe);
52static int gstcntlProcessUnlock(PVBOXSERVICECTRLPROCESS pProcess);
53/* Request handlers. */
54static DECLCALLBACK(int) gstcntlProcessOnInput(PVBOXSERVICECTRLPROCESS pThis, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, bool fPendingClose, void *pvBuf, uint32_t cbBuf);
55static DECLCALLBACK(int) gstcntlProcessOnOutput(PVBOXSERVICECTRLPROCESS pThis, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, uint32_t uHandle, uint32_t cbToRead, uint32_t uFlags);
56static DECLCALLBACK(int) gstcntlProcessOnTerm(PVBOXSERVICECTRLPROCESS pThis);
57
58/**
59 * Initialies the passed in thread data structure with the parameters given.
60 *
61 * @return IPRT status code.
62 * @param pProcess Process to initialize.
63 * @param pSession Guest session the process is bound to.
64 * @param pStartupInfo Startup information.
65 * @param u32ContextID The context ID bound to this request / command.
66 */
67static int gstcntlProcessInit(PVBOXSERVICECTRLPROCESS pProcess,
68 const PVBOXSERVICECTRLSESSION pSession,
69 const PVBOXSERVICECTRLPROCSTARTUPINFO pStartupInfo,
70 uint32_t u32ContextID)
71{
72 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
73 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
74 AssertPtrReturn(pStartupInfo, VERR_INVALID_POINTER);
75
76 /* General stuff. */
77 pProcess->hProcess = NIL_RTPROCESS;
78 pProcess->pSession = pSession;
79 pProcess->Node.pPrev = NULL;
80 pProcess->Node.pNext = NULL;
81
82 pProcess->fShutdown = false;
83 pProcess->fStarted = false;
84 pProcess->fStopped = false;
85
86 pProcess->uPID = 0; /* Don't have a PID yet. */
87 pProcess->cRefs = 0;
88 /*
89 * Use the initial context ID we got for starting
90 * the process to report back its status with the
91 * same context ID.
92 */
93 pProcess->uContextID = u32ContextID;
94 /*
95 * Note: pProcess->ClientID will be assigned when thread is started;
96 * every guest process has its own client ID to detect crashes on
97 * a per-guest-process level.
98 */
99
100 int rc = RTCritSectInit(&pProcess->CritSect);
101 if (RT_FAILURE(rc))
102 return rc;
103
104 pProcess->hPollSet = NIL_RTPOLLSET;
105 pProcess->hPipeStdInW = NIL_RTPIPE;
106 pProcess->hPipeStdOutR = NIL_RTPIPE;
107 pProcess->hPipeStdErrR = NIL_RTPIPE;
108 pProcess->hNotificationPipeW = NIL_RTPIPE;
109 pProcess->hNotificationPipeR = NIL_RTPIPE;
110
111 rc = RTReqQueueCreate(&pProcess->hReqQueue);
112 AssertReleaseRC(rc);
113
114 /* Copy over startup info. */
115 memcpy(&pProcess->StartupInfo, pStartupInfo, sizeof(VBOXSERVICECTRLPROCSTARTUPINFO));
116
117 /* Adjust timeout value. */
118 if ( pProcess->StartupInfo.uTimeLimitMS == UINT32_MAX
119 || pProcess->StartupInfo.uTimeLimitMS == 0)
120 pProcess->StartupInfo.uTimeLimitMS = RT_INDEFINITE_WAIT;
121
122 if (RT_FAILURE(rc)) /* Clean up on failure. */
123 GstCntlProcessFree(pProcess);
124 return rc;
125}
126
127
128/**
129 * Frees a guest process. On success, pProcess will be
130 * free'd and thus won't be available anymore.
131 *
132 * @return IPRT status code.
133 * @param pProcess Guest process to free.
134 */
135int GstCntlProcessFree(PVBOXSERVICECTRLPROCESS pProcess)
136{
137 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
138
139 VBoxServiceVerbose(3, "[PID %RU32]: Freeing (cRefs=%RU32)...\n",
140 pProcess->uPID, pProcess->cRefs);
141 Assert(pProcess->cRefs == 0);
142
143 /*
144 * Destroy other thread data.
145 */
146 if (RTCritSectIsInitialized(&pProcess->CritSect))
147 RTCritSectDelete(&pProcess->CritSect);
148
149 int rc = RTReqQueueDestroy(pProcess->hReqQueue);
150 AssertRC(rc);
151
152 /*
153 * Remove from list.
154 */
155 AssertPtr(pProcess->pSession);
156 rc = GstCntlSessionProcessRemove(pProcess->pSession, pProcess);
157 AssertRC(rc);
158
159 /*
160 * Destroy thread structure as final step.
161 */
162 RTMemFree(pProcess);
163 pProcess = NULL;
164
165 return VINF_SUCCESS;
166}
167
168
169/**
170 * Signals a guest process thread that we want it to shut down in
171 * a gentle way.
172 *
173 * @return IPRT status code.
174 * @param pProcess Process to stop.
175 */
176int GstCntlProcessStop(PVBOXSERVICECTRLPROCESS pProcess)
177{
178 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
179
180 VBoxServiceVerbose(3, "[PID %RU32]: Stopping ...\n",
181 pProcess->uPID);
182
183 /* Do *not* set pThread->fShutdown or other stuff here!
184 * The guest thread loop will clean up itself. */
185
186 return GstCntlProcessHandleTerm(pProcess);
187}
188
189
190/**
191 * Releases a previously acquired guest process (decreases the refcount).
192 *
193 * @param pProcess Process to unlock.
194 */
195void GstCntlProcessRelease(PVBOXSERVICECTRLPROCESS pProcess)
196{
197 AssertPtrReturnVoid(pProcess);
198
199 bool fShutdown = false;
200
201 int rc = RTCritSectEnter(&pProcess->CritSect);
202 if (RT_SUCCESS(rc))
203 {
204 Assert(pProcess->cRefs);
205 pProcess->cRefs--;
206 fShutdown = pProcess->fStopped; /* Has the process' thread been stopped? */
207
208 rc = RTCritSectLeave(&pProcess->CritSect);
209 AssertRC(rc);
210 }
211
212 if (fShutdown)
213 GstCntlProcessFree(pProcess);
214}
215
216
217/**
218 * Wait for a guest process thread to shut down.
219 *
220 * @return IPRT status code.
221 * @param pProcess Process to wait shutting down for.
222 * @param RTMSINTERVAL Timeout in ms to wait for shutdown.
223 * @param pRc Where to store the thread's return code. Optional.
224 */
225int GstCntlProcessWait(const PVBOXSERVICECTRLPROCESS pProcess,
226 RTMSINTERVAL msTimeout, int *pRc)
227{
228 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
229 /* pRc is optional. */
230
231 int rc = gstcntlProcessLock(pProcess);
232 if (RT_SUCCESS(rc))
233 {
234 VBoxServiceVerbose(2, "[PID %RU32]: Waiting for shutdown (%RU32ms) ...\n",
235 pProcess->uPID, msTimeout);
236
237 AssertMsgReturn(pProcess->fStarted,
238 ("Tried to wait on guest process=%p (PID %RU32) which has not been started yet\n",
239 pProcess, pProcess->uPID), VERR_INVALID_PARAMETER);
240
241 /* Guest process already has been stopped, no need to wait. */
242 if (!pProcess->fStopped)
243 {
244 /* Unlock process before waiting. */
245 rc = gstcntlProcessUnlock(pProcess);
246 AssertRC(rc);
247
248 /* Do the actual waiting. */
249 int rcThread;
250 Assert(pProcess->Thread != NIL_RTTHREAD);
251 rc = RTThreadWait(pProcess->Thread, msTimeout, &rcThread);
252 if (RT_FAILURE(rc))
253 {
254 VBoxServiceError("[PID %RU32]: Waiting for shutting down thread returned error rc=%Rrc\n",
255 pProcess->uPID, rc);
256 }
257 else
258 {
259 VBoxServiceVerbose(3, "[PID %RU32]: Thread shutdown complete, thread rc=%Rrc\n",
260 pProcess->uPID, rcThread);
261 if (pRc)
262 *pRc = rcThread;
263 }
264 }
265 else
266 {
267 VBoxServiceVerbose(3, "[PID %RU32]: Thread already shut down, no waiting needed\n",
268 pProcess->uPID);
269
270 int rc2 = gstcntlProcessUnlock(pProcess);
271 AssertRC(rc2);
272 }
273 }
274
275 VBoxServiceVerbose(3, "[PID %RU32]: Waiting resulted in rc=%Rrc\n",
276 pProcess->uPID, rc);
277 return rc;
278}
279
280
281/**
282 * Closes the stdin pipe of a guest process.
283 *
284 * @return IPRT status code.
285 * @param hPollSet The polling set.
286 * @param phStdInW The standard input pipe handle.
287 */
288static int gstcntlProcessPollsetCloseInput(PVBOXSERVICECTRLPROCESS pProcess,
289 PRTPIPE phStdInW)
290{
291 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
292 AssertPtrReturn(phStdInW, VERR_INVALID_POINTER);
293
294 int rc = RTPollSetRemove(pProcess->hPollSet, VBOXSERVICECTRLPIPEID_STDIN);
295 if (rc != VERR_POLL_HANDLE_ID_NOT_FOUND)
296 AssertRC(rc);
297
298 if (*phStdInW != NIL_RTPIPE)
299 {
300 rc = RTPipeClose(*phStdInW);
301 AssertRC(rc);
302 *phStdInW = NIL_RTPIPE;
303 }
304
305 return rc;
306}
307
308
309static const char* gstcntlProcessPollHandleToString(uint32_t idPollHnd)
310{
311 switch (idPollHnd)
312 {
313 case VBOXSERVICECTRLPIPEID_UNKNOWN:
314 return "unknown";
315 case VBOXSERVICECTRLPIPEID_STDIN:
316 return "stdin";
317 case VBOXSERVICECTRLPIPEID_STDIN_WRITABLE:
318 return "stdin_writable";
319 case VBOXSERVICECTRLPIPEID_STDOUT:
320 return "stdout";
321 case VBOXSERVICECTRLPIPEID_STDERR:
322 return "stderr";
323 case VBOXSERVICECTRLPIPEID_IPC_NOTIFY:
324 return "ipc_notify";
325 default:
326 break;
327 }
328
329 return "unknown";
330}
331
332
333/**
334 * Handle an error event on standard input.
335 *
336 * @return IPRT status code.
337 * @param pProcess Process to handle pollset for.
338 * @param fPollEvt The event mask returned by RTPollNoResume.
339 * @param phStdInW The standard input pipe handle.
340 */
341static int gstcntlProcessPollsetOnInput(PVBOXSERVICECTRLPROCESS pProcess,
342 uint32_t fPollEvt, PRTPIPE phStdInW)
343{
344 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
345
346 NOREF(fPollEvt);
347
348 return gstcntlProcessPollsetCloseInput(pProcess, phStdInW);
349}
350
351
352/**
353 * Handle pending output data or error on standard out or standard error.
354 *
355 * @returns IPRT status code from client send.
356 * @param pProcess Process to handle pollset for.
357 * @param fPollEvt The event mask returned by RTPollNoResume.
358 * @param phPipeR The pipe handle.
359 * @param idPollHnd The pipe ID to handle.
360 *
361 */
362static int gstcntlProcessHandleOutputError(PVBOXSERVICECTRLPROCESS pProcess,
363 uint32_t fPollEvt, PRTPIPE phPipeR, uint32_t idPollHnd)
364{
365 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
366
367 if (!phPipeR)
368 return VINF_SUCCESS;
369
370#ifdef DEBUG
371 VBoxServiceVerbose(4, "[PID %RU32]: Output error: idPollHnd=%s, fPollEvt=0x%x\n",
372 pProcess->uPID, gstcntlProcessPollHandleToString(idPollHnd), fPollEvt);
373#endif
374
375 /* Remove pipe from poll set. */
376 int rc2 = RTPollSetRemove(pProcess->hPollSet, idPollHnd);
377 AssertMsg(RT_SUCCESS(rc2) || rc2 == VERR_POLL_HANDLE_ID_NOT_FOUND, ("%Rrc\n", rc2));
378
379 bool fClosePipe = true; /* By default close the pipe. */
380
381 /* Check if there's remaining data to read from the pipe. */
382 if (*phPipeR != NIL_RTPIPE)
383 {
384 size_t cbReadable;
385 rc2 = RTPipeQueryReadable(*phPipeR, &cbReadable);
386 if ( RT_SUCCESS(rc2)
387 && cbReadable)
388 {
389#ifdef DEBUG
390 VBoxServiceVerbose(3, "[PID %RU32]: idPollHnd=%s has %zu bytes left, vetoing close\n",
391 pProcess->uPID, gstcntlProcessPollHandleToString(idPollHnd), cbReadable);
392#endif
393 /* Veto closing the pipe yet because there's still stuff to read
394 * from the pipe. This can happen on UNIX-y systems where on
395 * error/hangup there still can be data to be read out. */
396 fClosePipe = false;
397 }
398 }
399#ifdef DEBUG
400 else
401 VBoxServiceVerbose(3, "[PID %RU32]: idPollHnd=%s will be closed\n",
402 pProcess->uPID, gstcntlProcessPollHandleToString(idPollHnd));
403#endif
404
405 if ( *phPipeR != NIL_RTPIPE
406 && fClosePipe)
407 {
408 rc2 = RTPipeClose(*phPipeR);
409 AssertRC(rc2);
410 *phPipeR = NIL_RTPIPE;
411 }
412
413 return VINF_SUCCESS;
414}
415
416
417/**
418 * Handle pending output data or error on standard out or standard error.
419 *
420 * @returns IPRT status code from client send.
421 * @param pProcess Process to handle pollset for.
422 * @param fPollEvt The event mask returned by RTPollNoResume.
423 * @param phPipeR The pipe handle.
424 * @param idPollHnd The pipe ID to handle.
425 *
426 */
427static int gstcntlProcessPollsetOnOutput(PVBOXSERVICECTRLPROCESS pProcess,
428 uint32_t fPollEvt, PRTPIPE phPipeR, uint32_t idPollHnd)
429{
430 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
431
432#ifdef DEBUG
433 VBoxServiceVerbose(4, "[PID %RU32]: Output event phPipeR=%p, idPollHnd=%s, fPollEvt=0x%x\n",
434 pProcess->uPID, phPipeR, gstcntlProcessPollHandleToString(idPollHnd), fPollEvt);
435#endif
436
437 if (!phPipeR)
438 return VINF_SUCCESS;
439
440 int rc = VINF_SUCCESS;
441
442#ifdef DEBUG
443 if (*phPipeR != NIL_RTPIPE)
444 {
445 size_t cbReadable;
446 rc = RTPipeQueryReadable(*phPipeR, &cbReadable);
447 if ( RT_SUCCESS(rc)
448 && cbReadable)
449 {
450 VBoxServiceVerbose(4, "[PID %RU32]: Output event cbReadable=%zu\n",
451 pProcess->uPID, cbReadable);
452 }
453 }
454#endif
455
456#if 0
457 /* Push output to the host. */
458 if (fPollEvt & RTPOLL_EVT_READ)
459 {
460 size_t cbRead = 0;
461 uint8_t byData[_64K];
462 rc = RTPipeRead(*phPipeR,
463 byData, sizeof(byData), &cbRead);
464 VBoxServiceVerbose(4, "GstCntlProcessHandleOutputEvent cbRead=%u, rc=%Rrc\n",
465 cbRead, rc);
466
467 /* Make sure we go another poll round in case there was too much data
468 for the buffer to hold. */
469 fPollEvt &= RTPOLL_EVT_ERROR;
470 }
471#endif
472
473 if (fPollEvt & RTPOLL_EVT_ERROR)
474 rc = gstcntlProcessHandleOutputError(pProcess,
475 fPollEvt, phPipeR, idPollHnd);
476 return rc;
477}
478
479
480/**
481 * Execution loop which runs in a dedicated per-started-process thread and
482 * handles all pipe input/output and signalling stuff.
483 *
484 * @return IPRT status code.
485 * @param pProcess The guest process to handle.
486 */
487static int gstcntlProcessProcLoop(PVBOXSERVICECTRLPROCESS pProcess)
488{
489 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
490
491 int rc;
492 int rc2;
493 uint64_t const uMsStart = RTTimeMilliTS();
494 RTPROCSTATUS ProcessStatus = { 254, RTPROCEXITREASON_ABEND };
495 bool fProcessAlive = true;
496 bool fProcessTimedOut = false;
497 uint64_t MsProcessKilled = UINT64_MAX;
498 RTMSINTERVAL const cMsPollBase = pProcess->hPipeStdInW != NIL_RTPIPE
499 ? 100 /* Need to poll for input. */
500 : 1000; /* Need only poll for process exit and aborts. */
501 RTMSINTERVAL cMsPollCur = 0;
502
503 /*
504 * Assign PID to thread data.
505 * Also check if there already was a thread with the same PID and shut it down -- otherwise
506 * the first (stale) entry will be found and we get really weird results!
507 */
508 rc = gstcntlProcessAssignPID(pProcess, pProcess->hProcess /* Opaque PID handle */);
509 if (RT_FAILURE(rc))
510 {
511 VBoxServiceError("Unable to assign PID=%u, to new thread, rc=%Rrc\n",
512 pProcess->hProcess, rc);
513 return rc;
514 }
515
516 /*
517 * Before entering the loop, tell the host that we've started the guest
518 * and that it's now OK to send input to the process.
519 */
520 VBoxServiceVerbose(2, "[PID %RU32]: Process \"%s\" started, CID=%u, User=%s, cMsTimeout=%RU32\n",
521 pProcess->uPID, pProcess->StartupInfo.szCmd, pProcess->uContextID,
522 pProcess->StartupInfo.szUser, pProcess->StartupInfo.uTimeLimitMS);
523 VBGLR3GUESTCTRLCMDCTX ctxStart = { pProcess->uClientID, pProcess->uContextID };
524 rc = VbglR3GuestCtrlProcCbStatus(&ctxStart,
525 pProcess->uPID, PROC_STS_STARTED, 0 /* u32Flags */,
526 NULL /* pvData */, 0 /* cbData */);
527 if (rc == VERR_INTERRUPTED)
528 rc = VINF_SUCCESS; /* SIGCHLD send by quick childs! */
529 if (RT_FAILURE(rc))
530 VBoxServiceError("[PID %RU32]: Error reporting starting status to host, rc=%Rrc\n",
531 pProcess->uPID, rc);
532
533 /*
534 * Process input, output, the test pipe and client requests.
535 */
536 while ( RT_SUCCESS(rc)
537 && RT_UNLIKELY(!pProcess->fShutdown))
538 {
539 /*
540 * Wait/Process all pending events.
541 */
542 uint32_t idPollHnd;
543 uint32_t fPollEvt;
544 rc2 = RTPollNoResume(pProcess->hPollSet, cMsPollCur, &fPollEvt, &idPollHnd);
545 if (pProcess->fShutdown)
546 continue;
547
548 cMsPollCur = 0; /* No rest until we've checked everything. */
549
550 if (RT_SUCCESS(rc2))
551 {
552 switch (idPollHnd)
553 {
554 case VBOXSERVICECTRLPIPEID_STDIN:
555 rc = gstcntlProcessPollsetOnInput(pProcess, fPollEvt,
556 &pProcess->hPipeStdInW);
557 break;
558
559 case VBOXSERVICECTRLPIPEID_STDOUT:
560 rc = gstcntlProcessPollsetOnOutput(pProcess, fPollEvt,
561 &pProcess->hPipeStdOutR, idPollHnd);
562 break;
563
564 case VBOXSERVICECTRLPIPEID_STDERR:
565 rc = gstcntlProcessPollsetOnOutput(pProcess, fPollEvt,
566 &pProcess->hPipeStdOutR, idPollHnd);
567 break;
568
569 case VBOXSERVICECTRLPIPEID_IPC_NOTIFY:
570#ifdef DEBUG_andy
571 VBoxServiceVerbose(4, "[PID %RU32]: IPC notify\n", pProcess->uPID);
572#endif
573 rc2 = gstcntlProcessLock(pProcess);
574 if (RT_SUCCESS(rc2))
575 {
576 /* Drain the notification pipe. */
577 uint8_t abBuf[8];
578 size_t cbIgnore;
579 rc2 = RTPipeRead(pProcess->hNotificationPipeR,
580 abBuf, sizeof(abBuf), &cbIgnore);
581 if (RT_FAILURE(rc2))
582 VBoxServiceError("Draining IPC notification pipe failed with rc=%Rrc\n", rc2);
583
584 /* Process all pending requests. */
585 VBoxServiceVerbose(4, "[PID %RU32]: Processing pending requests ...\n",
586 pProcess->uPID);
587 Assert(pProcess->hReqQueue != NIL_RTREQQUEUE);
588 rc2 = RTReqQueueProcess(pProcess->hReqQueue,
589 0 /* Only process all pending requests, don't wait for new ones */);
590 if ( RT_FAILURE(rc2)
591 && rc2 != VERR_TIMEOUT)
592 VBoxServiceError("Processing requests failed with with rc=%Rrc\n", rc2);
593
594 int rc3 = gstcntlProcessUnlock(pProcess);
595 AssertRC(rc3);
596#ifdef DEBUG
597 VBoxServiceVerbose(4, "[PID %RU32]: Processing pending requests done, rc=%Rrc\n",
598 pProcess->uPID, rc2);
599#endif
600 }
601
602 break;
603
604 default:
605 AssertMsgFailed(("Unknown idPollHnd=%RU32\n", idPollHnd));
606 break;
607 }
608
609 if (RT_FAILURE(rc) || rc == VINF_EOF)
610 break; /* Abort command, or client dead or something. */
611 }
612#if 0
613 VBoxServiceVerbose(4, "[PID %RU32]: Polling done, pollRc=%Rrc, pollCnt=%RU32, idPollHnd=%s, rc=%Rrc, fProcessAlive=%RTbool, fShutdown=%RTbool\n",
614 pProcess->uPID, rc2, RTPollSetGetCount(hPollSet), gstcntlProcessPollHandleToString(idPollHnd), rc, fProcessAlive, pProcess->fShutdown);
615 VBoxServiceVerbose(4, "[PID %RU32]: stdOut=%s, stdErrR=%s\n",
616 pProcess->uPID,
617 *phStdOutR == NIL_RTPIPE ? "closed" : "open",
618 *phStdErrR == NIL_RTPIPE ? "closed" : "open");
619#endif
620 if (RT_UNLIKELY(pProcess->fShutdown))
621 break; /* We were asked to shutdown. */
622
623 /*
624 * Check for process death.
625 */
626 if (fProcessAlive)
627 {
628 rc2 = RTProcWaitNoResume(pProcess->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
629#if 0
630 VBoxServiceVerbose(4, "[PID %RU32]: RTProcWaitNoResume=%Rrc\n",
631 pProcess->uPID, rc2);
632#endif
633 if (RT_SUCCESS_NP(rc2))
634 {
635 fProcessAlive = false;
636 /* Note: Don't bail out here yet. First check in the next block below
637 * if all needed pipe outputs have been consumed. */
638 }
639 else
640 {
641 if (RT_UNLIKELY(rc2 == VERR_INTERRUPTED))
642 continue;
643 if (RT_UNLIKELY(rc2 == VERR_PROCESS_NOT_FOUND))
644 {
645 fProcessAlive = false;
646 ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
647 ProcessStatus.iStatus = 255;
648 AssertFailed();
649 }
650 else
651 AssertMsg(rc2 == VERR_PROCESS_RUNNING, ("%Rrc\n", rc2));
652 }
653 }
654
655 /*
656 * If the process has terminated and all output has been consumed,
657 * we should be heading out.
658 */
659 if (!fProcessAlive)
660 {
661 if ( fProcessTimedOut
662 || ( pProcess->hPipeStdOutR == NIL_RTPIPE
663 && pProcess->hPipeStdErrR == NIL_RTPIPE)
664 )
665 {
666 break;
667 }
668 }
669
670 /*
671 * Check for timed out, killing the process.
672 */
673 uint32_t cMilliesLeft = RT_INDEFINITE_WAIT;
674 if ( pProcess->StartupInfo.uTimeLimitMS != RT_INDEFINITE_WAIT
675 && pProcess->StartupInfo.uTimeLimitMS != 0)
676 {
677 uint64_t u64Now = RTTimeMilliTS();
678 uint64_t cMsElapsed = u64Now - uMsStart;
679 if (cMsElapsed >= pProcess->StartupInfo.uTimeLimitMS)
680 {
681 fProcessTimedOut = true;
682 if ( MsProcessKilled == UINT64_MAX
683 || u64Now - MsProcessKilled > 1000)
684 {
685 if (u64Now - MsProcessKilled > 20*60*1000)
686 break; /* Give up after 20 mins. */
687
688 VBoxServiceVerbose(3, "[PID %RU32]: Timed out (%RU64ms elapsed > %RU32ms timeout), killing ...\n",
689 pProcess->uPID, cMsElapsed, pProcess->StartupInfo.uTimeLimitMS);
690
691 rc2 = RTProcTerminate(pProcess->hProcess);
692 VBoxServiceVerbose(3, "[PID %RU32]: Killing process resulted in rc=%Rrc\n",
693 pProcess->uPID, rc2);
694 MsProcessKilled = u64Now;
695 continue;
696 }
697 cMilliesLeft = 10000;
698 }
699 else
700 cMilliesLeft = pProcess->StartupInfo.uTimeLimitMS - (uint32_t)cMsElapsed;
701 }
702
703 /* Reset the polling interval since we've done all pending work. */
704 cMsPollCur = fProcessAlive
705 ? cMsPollBase
706 : RT_MS_1MIN;
707 if (cMilliesLeft < cMsPollCur)
708 cMsPollCur = cMilliesLeft;
709 }
710
711 VBoxServiceVerbose(3, "[PID %RU32]: Loop ended: rc=%Rrc, fShutdown=%RTbool, fProcessAlive=%RTbool, fProcessTimedOut=%RTbool, MsProcessKilled=%RU64\n",
712 pProcess->uPID, rc, pProcess->fShutdown, fProcessAlive, fProcessTimedOut, MsProcessKilled, MsProcessKilled);
713 VBoxServiceVerbose(3, "[PID %RU32]: *phStdOutR=%s, *phStdErrR=%s\n",
714 pProcess->uPID,
715 pProcess->hPipeStdOutR == NIL_RTPIPE ? "closed" : "open",
716 pProcess->hPipeStdErrR == NIL_RTPIPE ? "closed" : "open");
717
718 /* Signal that this thread is in progress of shutting down. */
719 ASMAtomicXchgBool(&pProcess->fShutdown, true);
720
721 /*
722 * Try killing the process if it's still alive at this point.
723 */
724 if (fProcessAlive)
725 {
726 if (MsProcessKilled == UINT64_MAX)
727 {
728 VBoxServiceVerbose(2, "[PID %RU32]: Is still alive and not killed yet\n",
729 pProcess->uPID);
730
731 MsProcessKilled = RTTimeMilliTS();
732 rc2 = RTProcTerminate(pProcess->hProcess);
733 if (rc2 == VERR_NOT_FOUND)
734 {
735 fProcessAlive = false;
736 }
737 else if (RT_FAILURE(rc2))
738 VBoxServiceError("PID %RU32]: Killing process failed with rc=%Rrc\n",
739 pProcess->uPID, rc2);
740 RTThreadSleep(500);
741 }
742
743 for (int i = 0; i < 10 && fProcessAlive; i++)
744 {
745 VBoxServiceVerbose(4, "[PID %RU32]: Kill attempt %d/10: Waiting to exit ...\n",
746 pProcess->uPID, i + 1);
747 rc2 = RTProcWait(pProcess->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
748 if (RT_SUCCESS(rc2))
749 {
750 VBoxServiceVerbose(4, "[PID %RU32]: Kill attempt %d/10: Exited\n",
751 pProcess->uPID, i + 1);
752 fProcessAlive = false;
753 break;
754 }
755 if (i >= 5)
756 {
757 VBoxServiceVerbose(4, "[PID %RU32]: Kill attempt %d/10: Trying to terminate ...\n",
758 pProcess->uPID, i + 1);
759 rc2 = RTProcTerminate(pProcess->hProcess);
760 if ( RT_FAILURE(rc)
761 && rc2 != VERR_NOT_FOUND)
762 VBoxServiceError("PID %RU32]: Killing process failed with rc=%Rrc\n",
763 pProcess->uPID, rc2);
764 }
765 RTThreadSleep(i >= 5 ? 2000 : 500);
766 }
767
768 if (fProcessAlive)
769 VBoxServiceError("[PID %RU32]: Could not be killed\n", pProcess->uPID);
770 }
771
772 /*
773 * Shutdown procedure:
774 * - Set the pProcess->fShutdown indicator to let others know we're
775 * not accepting any new requests anymore.
776 * - After setting the indicator, try to process all outstanding
777 * requests to make sure they're getting delivered.
778 *
779 * Note: After removing the process from the session's list it's not
780 * even possible for the session anymore to control what's
781 * happening to this thread, so be careful and don't mess it up.
782 */
783
784 rc2 = gstcntlProcessLock(pProcess);
785 if (RT_SUCCESS(rc2))
786 {
787 VBoxServiceVerbose(3, "[PID %RU32]: Processing outstanding requests ...\n",
788 pProcess->uPID);
789
790 /* Process all pending requests (but don't wait for new ones). */
791 Assert(pProcess->hReqQueue != NIL_RTREQQUEUE);
792 rc2 = RTReqQueueProcess(pProcess->hReqQueue, 0 /* No timeout */);
793 if ( RT_FAILURE(rc2)
794 && rc2 != VERR_TIMEOUT)
795 VBoxServiceError("[PID %RU32]: Processing outstanding requests failed with with rc=%Rrc\n",
796 pProcess->uPID, rc2);
797
798 VBoxServiceVerbose(3, "[PID %RU32]: Processing outstanding requests done, rc=%Rrc\n",
799 pProcess->uPID, rc2);
800
801 rc2 = gstcntlProcessUnlock(pProcess);
802 AssertRC(rc2);
803 }
804
805 /*
806 * If we don't have a client problem (RT_FAILURE(rc)) we'll reply to the
807 * clients exec packet now.
808 */
809 if (RT_SUCCESS(rc))
810 {
811 uint32_t uStatus = PROC_STS_UNDEFINED;
812 uint32_t uFlags = 0;
813
814 if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX)
815 {
816 VBoxServiceVerbose(3, "[PID %RU32]: Timed out and got killed\n",
817 pProcess->uPID);
818 uStatus = PROC_STS_TOK;
819 }
820 else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX)
821 {
822 VBoxServiceVerbose(3, "[PID %RU32]: Timed out and did *not* get killed\n",
823 pProcess->uPID);
824 uStatus = PROC_STS_TOA;
825 }
826 else if (pProcess->fShutdown && (fProcessAlive || MsProcessKilled != UINT64_MAX))
827 {
828 VBoxServiceVerbose(3, "[PID %RU32]: Got terminated because system/service is about to shutdown\n",
829 pProcess->uPID);
830 uStatus = PROC_STS_DWN; /* Service is stopping, process was killed. */
831 uFlags = pProcess->StartupInfo.uFlags; /* Return handed-in execution flags back to the host. */
832 }
833 else if (fProcessAlive)
834 {
835 VBoxServiceError("[PID %RU32]: Is alive when it should not!\n",
836 pProcess->uPID);
837 }
838 else if (MsProcessKilled != UINT64_MAX)
839 {
840 VBoxServiceError("[PID %RU32]: Has been killed when it should not!\n",
841 pProcess->uPID);
842 }
843 else if (ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL)
844 {
845 VBoxServiceVerbose(3, "[PID %RU32]: Ended with RTPROCEXITREASON_NORMAL (Exit code: %d)\n",
846 pProcess->uPID, ProcessStatus.iStatus);
847
848 uStatus = PROC_STS_TEN;
849 uFlags = ProcessStatus.iStatus;
850 }
851 else if (ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL)
852 {
853 VBoxServiceVerbose(3, "[PID %RU32]: Ended with RTPROCEXITREASON_SIGNAL (Signal: %u)\n",
854 pProcess->uPID, ProcessStatus.iStatus);
855
856 uStatus = PROC_STS_TES;
857 uFlags = ProcessStatus.iStatus;
858 }
859 else if (ProcessStatus.enmReason == RTPROCEXITREASON_ABEND)
860 {
861 /* ProcessStatus.iStatus will be undefined. */
862 VBoxServiceVerbose(3, "[PID %RU32]: Ended with RTPROCEXITREASON_ABEND\n",
863 pProcess->uPID);
864
865 uStatus = PROC_STS_TEA;
866 uFlags = ProcessStatus.iStatus;
867 }
868 else
869 VBoxServiceVerbose(1, "[PID %RU32]: Handling process status %u not implemented\n",
870 pProcess->uPID, ProcessStatus.enmReason);
871
872 VBoxServiceVerbose(2, "[PID %RU32]: Ended, ClientID=%u, CID=%u, Status=%u, Flags=0x%x\n",
873 pProcess->uPID, pProcess->uClientID, pProcess->uContextID, uStatus, uFlags);
874
875 VBGLR3GUESTCTRLCMDCTX ctxEnd = { pProcess->uClientID, pProcess->uContextID };
876 rc2 = VbglR3GuestCtrlProcCbStatus(&ctxEnd,
877 pProcess->uPID, uStatus, uFlags,
878 NULL /* pvData */, 0 /* cbData */);
879 if ( RT_FAILURE(rc2)
880 && rc2 == VERR_NOT_FOUND)
881 VBoxServiceError("[PID %RU32]: Error reporting final status to host; rc=%Rrc\n",
882 pProcess->uPID, rc2);
883 }
884
885 VBoxServiceVerbose(3, "[PID %RU32]: Process loop returned with rc=%Rrc\n",
886 pProcess->uPID, rc);
887 return rc;
888}
889
890
891/**
892 * Initializes a pipe's handle and pipe object.
893 *
894 * @return IPRT status code.
895 * @param ph The pipe's handle to initialize.
896 * @param phPipe The pipe's object to initialize.
897 */
898static int gstcntlProcessInitPipe(PRTHANDLE ph, PRTPIPE phPipe)
899{
900 AssertPtrReturn(ph, VERR_INVALID_PARAMETER);
901 AssertPtrReturn(phPipe, VERR_INVALID_PARAMETER);
902
903 ph->enmType = RTHANDLETYPE_PIPE;
904 ph->u.hPipe = NIL_RTPIPE;
905 *phPipe = NIL_RTPIPE;
906
907 return VINF_SUCCESS;
908}
909
910
911/**
912 * Sets up the redirection / pipe / nothing for one of the standard handles.
913 *
914 * @returns IPRT status code. No client replies made.
915 * @param pszHowTo How to set up this standard handle.
916 * @param fd Which standard handle it is (0 == stdin, 1 ==
917 * stdout, 2 == stderr).
918 * @param ph The generic handle that @a pph may be set
919 * pointing to. Always set.
920 * @param pph Pointer to the RTProcCreateExec argument.
921 * Always set.
922 * @param phPipe Where to return the end of the pipe that we
923 * should service.
924 */
925static int gstcntlProcessSetupPipe(const char *pszHowTo, int fd,
926 PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe)
927{
928 AssertPtrReturn(ph, VERR_INVALID_POINTER);
929 AssertPtrReturn(pph, VERR_INVALID_POINTER);
930 AssertPtrReturn(phPipe, VERR_INVALID_POINTER);
931
932 int rc;
933
934 ph->enmType = RTHANDLETYPE_PIPE;
935 ph->u.hPipe = NIL_RTPIPE;
936 *pph = NULL;
937 *phPipe = NIL_RTPIPE;
938
939 if (!strcmp(pszHowTo, "|"))
940 {
941 /*
942 * Setup a pipe for forwarding to/from the client.
943 * The ph union struct will be filled with a pipe read/write handle
944 * to represent the "other" end to phPipe.
945 */
946 if (fd == 0) /* stdin? */
947 {
948 /* Connect a wrtie pipe specified by phPipe to stdin. */
949 rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ);
950 }
951 else /* stdout or stderr? */
952 {
953 /* Connect a read pipe specified by phPipe to stdout or stderr. */
954 rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE);
955 }
956
957 if (RT_FAILURE(rc))
958 return rc;
959
960 ph->enmType = RTHANDLETYPE_PIPE;
961 *pph = ph;
962 }
963 else if (!strcmp(pszHowTo, "/dev/null"))
964 {
965 /*
966 * Redirect to/from /dev/null.
967 */
968 RTFILE hFile;
969 rc = RTFileOpenBitBucket(&hFile, fd == 0 ? RTFILE_O_READ : RTFILE_O_WRITE);
970 if (RT_FAILURE(rc))
971 return rc;
972
973 ph->enmType = RTHANDLETYPE_FILE;
974 ph->u.hFile = hFile;
975 *pph = ph;
976 }
977 else /* Add other piping stuff here. */
978 rc = VINF_SUCCESS; /* Same as parent (us). */
979
980 return rc;
981}
982
983
984/**
985 * Expands a file name / path to its real content. This only works on Windows
986 * for now (e.g. translating "%TEMP%\foo.exe" to "C:\Windows\Temp" when starting
987 * with system / administrative rights).
988 *
989 * @return IPRT status code.
990 * @param pszPath Path to resolve.
991 * @param pszExpanded Pointer to string to store the resolved path in.
992 * @param cbExpanded Size (in bytes) of string to store the resolved path.
993 */
994static int gstcntlProcessMakeFullPath(const char *pszPath, char *pszExpanded, size_t cbExpanded)
995{
996 int rc = VINF_SUCCESS;
997#ifdef RT_OS_WINDOWS
998 if (!ExpandEnvironmentStrings(pszPath, pszExpanded, cbExpanded))
999 rc = RTErrConvertFromWin32(GetLastError());
1000#else
1001 /* No expansion for non-Windows yet. */
1002 rc = RTStrCopy(pszExpanded, cbExpanded, pszPath);
1003#endif
1004#ifdef DEBUG
1005 VBoxServiceVerbose(3, "VBoxServiceControlExecMakeFullPath: %s -> %s\n",
1006 pszPath, pszExpanded);
1007#endif
1008 return rc;
1009}
1010
1011
1012/**
1013 * Resolves the full path of a specified executable name. This function also
1014 * resolves internal VBoxService tools to its appropriate executable path + name if
1015 * VBOXSERVICE_NAME is specified as pszFileName.
1016 *
1017 * @return IPRT status code.
1018 * @param pszFileName File name to resolve.
1019 * @param pszResolved Pointer to a string where the resolved file name will be stored.
1020 * @param cbResolved Size (in bytes) of resolved file name string.
1021 */
1022static int gstcntlProcessResolveExecutable(const char *pszFileName,
1023 char *pszResolved, size_t cbResolved)
1024{
1025 AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
1026 AssertPtrReturn(pszResolved, VERR_INVALID_POINTER);
1027 AssertReturn(cbResolved, VERR_INVALID_PARAMETER);
1028
1029 int rc = VINF_SUCCESS;
1030
1031 char szPathToResolve[RTPATH_MAX];
1032 if ( (g_pszProgName && (RTStrICmp(pszFileName, g_pszProgName) == 0))
1033 || !RTStrICmp(pszFileName, VBOXSERVICE_NAME))
1034 {
1035 /* Resolve executable name of this process. */
1036 if (!RTProcGetExecutablePath(szPathToResolve, sizeof(szPathToResolve)))
1037 rc = VERR_FILE_NOT_FOUND;
1038 }
1039 else
1040 {
1041 /* Take the raw argument to resolve. */
1042 rc = RTStrCopy(szPathToResolve, sizeof(szPathToResolve), pszFileName);
1043 }
1044
1045 if (RT_SUCCESS(rc))
1046 {
1047 rc = gstcntlProcessMakeFullPath(szPathToResolve, pszResolved, cbResolved);
1048 if (RT_SUCCESS(rc))
1049 VBoxServiceVerbose(3, "Looked up executable: %s -> %s\n",
1050 pszFileName, pszResolved);
1051 }
1052
1053 if (RT_FAILURE(rc))
1054 VBoxServiceError("Failed to lookup executable \"%s\" with rc=%Rrc\n",
1055 pszFileName, rc);
1056 return rc;
1057}
1058
1059
1060/**
1061 * Constructs the argv command line by resolving environment variables
1062 * and relative paths.
1063 *
1064 * @return IPRT status code.
1065 * @param pszArgv0 First argument (argv0), either original or modified version. Optional.
1066 * @param papszArgs Original argv command line from the host, starting at argv[1].
1067 * @param ppapszArgv Pointer to a pointer with the new argv command line.
1068 * Needs to be freed with RTGetOptArgvFree.
1069 */
1070static int gstcntlProcessAllocateArgv(const char *pszArgv0,
1071 const char * const *papszArgs,
1072 bool fExpandArgs, char ***ppapszArgv)
1073{
1074 AssertPtrReturn(ppapszArgv, VERR_INVALID_POINTER);
1075
1076 VBoxServiceVerbose(3, "GstCntlProcessPrepareArgv: pszArgv0=%p, papszArgs=%p, fExpandArgs=%RTbool, ppapszArgv=%p\n",
1077 pszArgv0, papszArgs, fExpandArgs, ppapszArgv);
1078
1079 int rc = VINF_SUCCESS;
1080 uint32_t cArgs;
1081 for (cArgs = 0; papszArgs[cArgs]; cArgs++)
1082 {
1083 if (cArgs >= UINT32_MAX - 2)
1084 return VERR_BUFFER_OVERFLOW;
1085 }
1086
1087 /* Allocate new argv vector (adding + 2 for argv0 + termination). */
1088 size_t cbSize = (cArgs + 2) * sizeof(char*);
1089 char **papszNewArgv = (char**)RTMemAlloc(cbSize);
1090 if (!papszNewArgv)
1091 return VERR_NO_MEMORY;
1092
1093#ifdef DEBUG
1094 VBoxServiceVerbose(3, "GstCntlProcessAllocateArgv: cbSize=%RU32, cArgs=%RU32\n",
1095 cbSize, cArgs);
1096#endif
1097
1098 size_t i = 0; /* Keep the argument counter in scope for cleaning up on failure. */
1099
1100 rc = RTStrDupEx(&papszNewArgv[0], pszArgv0);
1101 if (RT_SUCCESS(rc))
1102 {
1103 for (; i < cArgs; i++)
1104 {
1105 char *pszArg;
1106#if 0 /* Arguments expansion -- untested. */
1107 if (fExpandArgs)
1108 {
1109 /* According to MSDN the limit on older Windows version is 32K, whereas
1110 * Vista+ there are no limits anymore. We still stick to 4K. */
1111 char szExpanded[_4K];
1112# ifdef RT_OS_WINDOWS
1113 if (!ExpandEnvironmentStrings(papszArgs[i], szExpanded, sizeof(szExpanded)))
1114 rc = RTErrConvertFromWin32(GetLastError());
1115# else
1116 /* No expansion for non-Windows yet. */
1117 rc = RTStrCopy(papszArgs[i], sizeof(szExpanded), szExpanded);
1118# endif
1119 if (RT_SUCCESS(rc))
1120 rc = RTStrDupEx(&pszArg, szExpanded);
1121 }
1122 else
1123#endif
1124 rc = RTStrDupEx(&pszArg, papszArgs[i]);
1125
1126 if (RT_FAILURE(rc))
1127 break;
1128
1129 papszNewArgv[i + 1] = pszArg;
1130 }
1131
1132 if (RT_SUCCESS(rc))
1133 {
1134 /* Terminate array. */
1135 papszNewArgv[cArgs + 1] = NULL;
1136
1137 *ppapszArgv = papszNewArgv;
1138 }
1139 }
1140
1141 if (RT_FAILURE(rc))
1142 {
1143 for (i; i > 0; i--)
1144 RTStrFree(papszNewArgv[i]);
1145 RTMemFree(papszNewArgv);
1146 }
1147
1148 return rc;
1149}
1150
1151
1152/**
1153 * Assigns a valid PID to a guest control thread and also checks if there already was
1154 * another (stale) guest process which was using that PID before and destroys it.
1155 *
1156 * @return IPRT status code.
1157 * @param pProcess Process to assign PID to.
1158 * @param uPID PID to assign to the specified guest control execution thread.
1159 */
1160int gstcntlProcessAssignPID(PVBOXSERVICECTRLPROCESS pProcess, uint32_t uPID)
1161{
1162 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1163 AssertReturn(uPID, VERR_INVALID_PARAMETER);
1164
1165 AssertPtr(pProcess->pSession);
1166 int rc = RTCritSectEnter(&pProcess->pSession->CritSect);
1167 if (RT_SUCCESS(rc))
1168 {
1169 /* Search old threads using the desired PID and shut them down completely -- it's
1170 * not used anymore. */
1171 PVBOXSERVICECTRLPROCESS pProcessCur;
1172 bool fTryAgain;
1173 do
1174 {
1175 fTryAgain = false;
1176 RTListForEach(&pProcess->pSession->lstProcesses, pProcessCur, VBOXSERVICECTRLPROCESS, Node)
1177 {
1178 if (pProcessCur->uPID == uPID)
1179 {
1180 Assert(pProcessCur != pProcess); /* can't happen */
1181 uint32_t uTriedPID = uPID;
1182 uPID += 391939;
1183 VBoxServiceVerbose(2, "PID %RU32 was used before (process %p), trying again with %RU32 ...\n",
1184 uTriedPID, pProcessCur, uPID);
1185 fTryAgain = true;
1186 break;
1187 }
1188 }
1189 } while (fTryAgain);
1190
1191 /* Assign PID to current thread. */
1192 pProcess->uPID = uPID;
1193
1194 rc = RTCritSectLeave(&pProcess->pSession->CritSect);
1195 AssertRC(rc);
1196 }
1197
1198 return rc;
1199}
1200
1201
1202void gstcntlProcessFreeArgv(char **papszArgv)
1203{
1204 if (papszArgv)
1205 {
1206 size_t i = 0;
1207 while (papszArgv[i])
1208 RTStrFree(papszArgv[i++]);
1209 RTMemFree(papszArgv);
1210 }
1211}
1212
1213
1214/**
1215 * Helper function to create/start a process on the guest.
1216 *
1217 * @return IPRT status code.
1218 * @param pszExec Full qualified path of process to start (without arguments).
1219 * @param papszArgs Pointer to array of command line arguments.
1220 * @param hEnv Handle to environment block to use.
1221 * @param fFlags Process execution flags.
1222 * @param phStdIn Handle for the process' stdin pipe.
1223 * @param phStdOut Handle for the process' stdout pipe.
1224 * @param phStdErr Handle for the process' stderr pipe.
1225 * @param pszAsUser User name (account) to start the process under.
1226 * @param pszPassword Password of the specified user.
1227 * @param phProcess Pointer which will receive the process handle after
1228 * successful process start.
1229 */
1230static int gstcntlProcessCreateProcess(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
1231 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
1232 const char *pszPassword, PRTPROCESS phProcess)
1233{
1234 AssertPtrReturn(pszExec, VERR_INVALID_PARAMETER);
1235 AssertPtrReturn(papszArgs, VERR_INVALID_PARAMETER);
1236 AssertPtrReturn(phProcess, VERR_INVALID_PARAMETER);
1237
1238 int rc = VINF_SUCCESS;
1239 char szExecExp[RTPATH_MAX];
1240
1241 /* Do we need to expand environment variables in arguments? */
1242 bool fExpandArgs = (fFlags & EXECUTEPROCESSFLAG_EXPAND_ARGUMENTS) ? true : false;
1243
1244#ifdef RT_OS_WINDOWS
1245 /*
1246 * If sysprep should be executed do this in the context of VBoxService, which
1247 * (usually, if started by SCM) has administrator rights. Because of that a UI
1248 * won't be shown (doesn't have a desktop).
1249 */
1250 if (!RTStrICmp(pszExec, "sysprep"))
1251 {
1252 /* Use a predefined sysprep path as default. */
1253 char szSysprepCmd[RTPATH_MAX] = "C:\\sysprep\\sysprep.exe";
1254 /** @todo Check digital signature of file above before executing it? */
1255
1256 /*
1257 * On Windows Vista (and up) sysprep is located in "system32\\Sysprep\\sysprep.exe",
1258 * so detect the OS and use a different path.
1259 */
1260 OSVERSIONINFOEX OSInfoEx;
1261 RT_ZERO(OSInfoEx);
1262 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
1263 BOOL fRet = GetVersionEx((LPOSVERSIONINFO) &OSInfoEx);
1264 if ( fRet
1265 && OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
1266 && OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
1267 {
1268 rc = RTEnvGetEx(RTENV_DEFAULT, "windir", szSysprepCmd, sizeof(szSysprepCmd), NULL);
1269#ifndef RT_ARCH_AMD64
1270 /* Don't execute 64-bit sysprep from a 32-bit service host! */
1271 char szSysWow64[RTPATH_MAX];
1272 if (RTStrPrintf(szSysWow64, sizeof(szSysWow64), "%s", szSysprepCmd))
1273 {
1274 rc = RTPathAppend(szSysWow64, sizeof(szSysWow64), "SysWow64");
1275 AssertRC(rc);
1276 }
1277 if ( RT_SUCCESS(rc)
1278 && RTPathExists(szSysWow64))
1279 VBoxServiceVerbose(0, "Warning: This service is 32-bit; could not execute sysprep on 64-bit OS!\n");
1280#endif
1281 if (RT_SUCCESS(rc))
1282 rc = RTPathAppend(szSysprepCmd, sizeof(szSysprepCmd), "system32\\Sysprep\\sysprep.exe");
1283 if (RT_SUCCESS(rc))
1284 RTPathChangeToDosSlashes(szSysprepCmd, false /* No forcing necessary */);
1285
1286 if (RT_FAILURE(rc))
1287 VBoxServiceError("Failed to detect sysrep location, rc=%Rrc\n", rc);
1288 }
1289 else if (!fRet)
1290 VBoxServiceError("Failed to retrieve OS information, last error=%ld\n", GetLastError());
1291
1292 VBoxServiceVerbose(3, "Sysprep executable is: %s\n", szSysprepCmd);
1293
1294 if (RT_SUCCESS(rc))
1295 {
1296 char **papszArgsExp;
1297 rc = gstcntlProcessAllocateArgv(szSysprepCmd /* argv0 */, papszArgs,
1298 fExpandArgs, &papszArgsExp);
1299 if (RT_SUCCESS(rc))
1300 {
1301 /* As we don't specify credentials for the sysprep process, it will
1302 * run under behalf of the account VBoxService was started under, most
1303 * likely local system. */
1304 rc = RTProcCreateEx(szSysprepCmd, papszArgsExp, hEnv, 0 /* fFlags */,
1305 phStdIn, phStdOut, phStdErr, NULL /* pszAsUser */,
1306 NULL /* pszPassword */, phProcess);
1307 gstcntlProcessFreeArgv(papszArgsExp);
1308 }
1309 }
1310
1311 if (RT_FAILURE(rc))
1312 VBoxServiceVerbose(3, "Starting sysprep returned rc=%Rrc\n", rc);
1313
1314 return rc;
1315 }
1316#endif /* RT_OS_WINDOWS */
1317
1318#ifdef VBOXSERVICE_TOOLBOX
1319 if (RTStrStr(pszExec, "vbox_") == pszExec)
1320 {
1321 /* We want to use the internal toolbox (all internal
1322 * tools are starting with "vbox_" (e.g. "vbox_cat"). */
1323 rc = gstcntlProcessResolveExecutable(VBOXSERVICE_NAME, szExecExp, sizeof(szExecExp));
1324 }
1325 else
1326 {
1327#endif
1328 /*
1329 * Do the environment variables expansion on executable and arguments.
1330 */
1331 rc = gstcntlProcessResolveExecutable(pszExec, szExecExp, sizeof(szExecExp));
1332#ifdef VBOXSERVICE_TOOLBOX
1333 }
1334#endif
1335 if (RT_SUCCESS(rc))
1336 {
1337 char **papszArgsExp;
1338 rc = gstcntlProcessAllocateArgv(pszExec /* Always use the unmodified executable name as argv0. */,
1339 papszArgs /* Append the rest of the argument vector (if any). */,
1340 fExpandArgs, &papszArgsExp);
1341 if (RT_FAILURE(rc))
1342 {
1343 /* Don't print any arguments -- may contain passwords or other sensible data! */
1344 VBoxServiceError("Could not prepare arguments, rc=%Rrc\n", rc);
1345 }
1346 else
1347 {
1348 uint32_t uProcFlags = 0;
1349 if (fFlags)
1350 {
1351 if (fFlags & EXECUTEPROCESSFLAG_HIDDEN)
1352 uProcFlags |= RTPROC_FLAGS_HIDDEN;
1353 if (fFlags & EXECUTEPROCESSFLAG_NO_PROFILE)
1354 uProcFlags |= RTPROC_FLAGS_NO_PROFILE;
1355 }
1356
1357 /* If no user name specified run with current credentials (e.g.
1358 * full service/system rights). This is prohibited via official Main API!
1359 *
1360 * Otherwise use the RTPROC_FLAGS_SERVICE to use some special authentication
1361 * code (at least on Windows) for running processes as different users
1362 * started from our system service. */
1363 if (pszAsUser && *pszAsUser)
1364 uProcFlags |= RTPROC_FLAGS_SERVICE;
1365#ifdef DEBUG
1366 VBoxServiceVerbose(3, "Command: %s\n", szExecExp);
1367 for (size_t i = 0; papszArgsExp[i]; i++)
1368 VBoxServiceVerbose(3, "\targv[%ld]: %s\n", i, papszArgsExp[i]);
1369#endif
1370 VBoxServiceVerbose(3, "Starting process \"%s\" ...\n", szExecExp);
1371
1372 /* Do normal execution. */
1373 rc = RTProcCreateEx(szExecExp, papszArgsExp, hEnv, uProcFlags,
1374 phStdIn, phStdOut, phStdErr,
1375 pszAsUser && *pszAsUser ? pszAsUser : NULL,
1376 pszPassword && *pszPassword ? pszPassword : NULL,
1377 phProcess);
1378
1379 VBoxServiceVerbose(3, "Starting process \"%s\" returned rc=%Rrc\n",
1380 szExecExp, rc);
1381
1382 gstcntlProcessFreeArgv(papszArgsExp);
1383 }
1384 }
1385 return rc;
1386}
1387
1388
1389#ifdef DEBUG
1390static int gstcntlProcessDumpToFile(const char *pszFileName, void *pvBuf, size_t cbBuf)
1391{
1392 AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
1393 AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
1394
1395 if (!cbBuf)
1396 return VINF_SUCCESS;
1397
1398 char szFile[RTPATH_MAX];
1399
1400 int rc = RTPathTemp(szFile, sizeof(szFile));
1401 if (RT_SUCCESS(rc))
1402 rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
1403
1404 if (RT_SUCCESS(rc))
1405 {
1406 VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
1407
1408 RTFILE fh;
1409 rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
1410 if (RT_SUCCESS(rc))
1411 {
1412 rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
1413 RTFileClose(fh);
1414 }
1415 }
1416
1417 return rc;
1418}
1419#endif
1420
1421
1422/**
1423 * The actual worker routine (loop) for a started guest process.
1424 *
1425 * @return IPRT status code.
1426 * @param PVBOXSERVICECTRLPROCESS Guest process.
1427 */
1428static int gstcntlProcessProcessWorker(PVBOXSERVICECTRLPROCESS pProcess)
1429{
1430 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1431 VBoxServiceVerbose(3, "Thread of process pThread=0x%p = \"%s\" started\n",
1432 pProcess, pProcess->StartupInfo.szCmd);
1433
1434 int rc = VbglR3GuestCtrlConnect(&pProcess->uClientID);
1435 if (RT_FAILURE(rc))
1436 {
1437 VBoxServiceError("Process thread \"%s\" (%p) failed to connect to the guest control service, rc=%Rrc\n",
1438 pProcess->StartupInfo.szCmd, pProcess, rc);
1439 RTThreadUserSignal(RTThreadSelf());
1440 return rc;
1441 }
1442
1443 VBoxServiceVerbose(3, "Guest process \"%s\" got client ID=%u, flags=0x%x\n",
1444 pProcess->StartupInfo.szCmd, pProcess->uClientID, pProcess->StartupInfo.uFlags);
1445
1446 /* The process thread is not interested in receiving any commands;
1447 * tell the host service. */
1448 rc = VbglR3GuestCtrlMsgFilterSet(pProcess->uClientID, 0 /* Skip all */,
1449 0 /* Filter mask to add */, 0 /* Filter mask to remove */);
1450 if (RT_FAILURE(rc))
1451 {
1452 VBoxServiceError("Unable to set message filter, rc=%Rrc\n", rc);
1453 /* Non-critical. */
1454 }
1455
1456 rc = GstCntlSessionProcessAdd(pProcess->pSession, pProcess);
1457 if (RT_FAILURE(rc))
1458 {
1459 VBoxServiceError("Errorwhile adding guest process \"%s\" (%p) to session process list, rc=%Rrc\n",
1460 pProcess->StartupInfo.szCmd, pProcess, rc);
1461 RTThreadUserSignal(RTThreadSelf());
1462 return rc;
1463 }
1464
1465 bool fSignalled = false; /* Indicator whether we signalled the thread user event already. */
1466
1467 /*
1468 * Prepare argument list.
1469 */
1470 char **papszArgs;
1471 uint32_t uNumArgs = 0; /* Initialize in case of RTGetOptArgvFromString() is failing ... */
1472 rc = RTGetOptArgvFromString(&papszArgs, (int*)&uNumArgs,
1473 (pProcess->StartupInfo.uNumArgs > 0) ? pProcess->StartupInfo.szArgs : "", NULL);
1474 /* Did we get the same result? */
1475 Assert(pProcess->StartupInfo.uNumArgs == uNumArgs);
1476
1477 /*
1478 * Prepare environment variables list.
1479 */
1480 char **papszEnv = NULL;
1481 uint32_t uNumEnvVars = 0; /* Initialize in case of failing ... */
1482 if (RT_SUCCESS(rc))
1483 {
1484 /* Prepare environment list. */
1485 if (pProcess->StartupInfo.uNumEnvVars)
1486 {
1487 papszEnv = (char **)RTMemAlloc(pProcess->StartupInfo.uNumEnvVars * sizeof(char*));
1488 AssertPtr(papszEnv);
1489 uNumEnvVars = pProcess->StartupInfo.uNumEnvVars;
1490
1491 const char *pszCur = pProcess->StartupInfo.szEnv;
1492 uint32_t i = 0;
1493 uint32_t cbLen = 0;
1494 while (cbLen < pProcess->StartupInfo.cbEnv)
1495 {
1496 /* sanity check */
1497 if (i >= pProcess->StartupInfo.uNumEnvVars)
1498 {
1499 rc = VERR_INVALID_PARAMETER;
1500 break;
1501 }
1502 int cbStr = RTStrAPrintf(&papszEnv[i++], "%s", pszCur);
1503 if (cbStr < 0)
1504 {
1505 rc = VERR_NO_STR_MEMORY;
1506 break;
1507 }
1508 pszCur += cbStr + 1; /* Skip terminating '\0' */
1509 cbLen += cbStr + 1; /* Skip terminating '\0' */
1510 }
1511 Assert(i == pProcess->StartupInfo.uNumEnvVars);
1512 }
1513 }
1514
1515 /*
1516 * Create the environment.
1517 */
1518 RTENV hEnv;
1519 if (RT_SUCCESS(rc))
1520 rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
1521 if (RT_SUCCESS(rc))
1522 {
1523 size_t i;
1524 for (i = 0; i < uNumEnvVars && papszEnv; i++)
1525 {
1526 rc = RTEnvPutEx(hEnv, papszEnv[i]);
1527 if (RT_FAILURE(rc))
1528 break;
1529 }
1530 if (RT_SUCCESS(rc))
1531 {
1532 /*
1533 * Setup the redirection of the standard stuff.
1534 */
1535 /** @todo consider supporting: gcc stuff.c >file 2>&1. */
1536 RTHANDLE hStdIn;
1537 PRTHANDLE phStdIn;
1538 rc = gstcntlProcessSetupPipe("|", 0 /*STDIN_FILENO*/,
1539 &hStdIn, &phStdIn, &pProcess->hPipeStdInW);
1540 if (RT_SUCCESS(rc))
1541 {
1542 RTHANDLE hStdOut;
1543 PRTHANDLE phStdOut;
1544 rc = gstcntlProcessSetupPipe( (pProcess->StartupInfo.uFlags & EXECUTEPROCESSFLAG_WAIT_STDOUT)
1545 ? "|" : "/dev/null",
1546 1 /*STDOUT_FILENO*/,
1547 &hStdOut, &phStdOut, &pProcess->hPipeStdOutR);
1548 if (RT_SUCCESS(rc))
1549 {
1550 RTHANDLE hStdErr;
1551 PRTHANDLE phStdErr;
1552 rc = gstcntlProcessSetupPipe( (pProcess->StartupInfo.uFlags & EXECUTEPROCESSFLAG_WAIT_STDERR)
1553 ? "|" : "/dev/null",
1554 2 /*STDERR_FILENO*/,
1555 &hStdErr, &phStdErr, &pProcess->hPipeStdErrR);
1556 if (RT_SUCCESS(rc))
1557 {
1558 /*
1559 * Create a poll set for the pipes and let the
1560 * transport layer add stuff to it as well.
1561 */
1562 rc = RTPollSetCreate(&pProcess->hPollSet);
1563 if (RT_SUCCESS(rc))
1564 {
1565 uint32_t uFlags = RTPOLL_EVT_ERROR;
1566#if 0
1567 /* Add reading event to pollset to get some more information. */
1568 uFlags |= RTPOLL_EVT_READ;
1569#endif
1570 /* Stdin. */
1571 if (RT_SUCCESS(rc))
1572 rc = RTPollSetAddPipe(pProcess->hPollSet,
1573 pProcess->hPipeStdInW, RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDIN);
1574 /* Stdout. */
1575 if (RT_SUCCESS(rc))
1576 rc = RTPollSetAddPipe(pProcess->hPollSet,
1577 pProcess->hPipeStdOutR, uFlags, VBOXSERVICECTRLPIPEID_STDOUT);
1578 /* Stderr. */
1579 if (RT_SUCCESS(rc))
1580 rc = RTPollSetAddPipe(pProcess->hPollSet,
1581 pProcess->hPipeStdErrR, uFlags, VBOXSERVICECTRLPIPEID_STDERR);
1582 /* IPC notification pipe. */
1583 if (RT_SUCCESS(rc))
1584 rc = RTPipeCreate(&pProcess->hNotificationPipeR, &pProcess->hNotificationPipeW, 0 /* Flags */);
1585 if (RT_SUCCESS(rc))
1586 rc = RTPollSetAddPipe(pProcess->hPollSet,
1587 pProcess->hNotificationPipeR, RTPOLL_EVT_READ, VBOXSERVICECTRLPIPEID_IPC_NOTIFY);
1588 if (RT_SUCCESS(rc))
1589 {
1590 AssertPtr(pProcess->pSession);
1591 bool fNeedsImpersonation = !(pProcess->pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_FORK);
1592
1593 rc = gstcntlProcessCreateProcess(pProcess->StartupInfo.szCmd, papszArgs, hEnv, pProcess->StartupInfo.uFlags,
1594 phStdIn, phStdOut, phStdErr,
1595 fNeedsImpersonation ? pProcess->StartupInfo.szUser : NULL,
1596 fNeedsImpersonation ? pProcess->StartupInfo.szPassword : NULL,
1597 &pProcess->hProcess);
1598 if (RT_FAILURE(rc))
1599 VBoxServiceError("Error starting process, rc=%Rrc\n", rc);
1600 /*
1601 * Tell the session thread that it can continue
1602 * spawning guest processes. This needs to be done after the new
1603 * process has been started because otherwise signal handling
1604 * on (Open) Solaris does not work correctly (see @bugref{5068}).
1605 */
1606 int rc2 = RTThreadUserSignal(RTThreadSelf());
1607 if (RT_SUCCESS(rc))
1608 rc = rc2;
1609 fSignalled = true;
1610
1611 if (RT_SUCCESS(rc))
1612 {
1613 /*
1614 * Close the child ends of any pipes and redirected files.
1615 */
1616 rc2 = RTHandleClose(phStdIn); AssertRC(rc2);
1617 phStdIn = NULL;
1618 rc2 = RTHandleClose(phStdOut); AssertRC(rc2);
1619 phStdOut = NULL;
1620 rc2 = RTHandleClose(phStdErr); AssertRC(rc2);
1621 phStdErr = NULL;
1622
1623 /* Enter the process main loop. */
1624 rc = gstcntlProcessProcLoop(pProcess);
1625
1626 /*
1627 * The handles that are no longer in the set have
1628 * been closed by the above call in order to prevent
1629 * the guest from getting stuck accessing them.
1630 * So, NIL the handles to avoid closing them again.
1631 */
1632 if (RT_FAILURE(RTPollSetQueryHandle(pProcess->hPollSet,
1633 VBOXSERVICECTRLPIPEID_IPC_NOTIFY, NULL)))
1634 {
1635 pProcess->hNotificationPipeR = NIL_RTPIPE;
1636 pProcess->hNotificationPipeW = NIL_RTPIPE;
1637 }
1638 if (RT_FAILURE(RTPollSetQueryHandle(pProcess->hPollSet,
1639 VBOXSERVICECTRLPIPEID_STDERR, NULL)))
1640 pProcess->hPipeStdErrR = NIL_RTPIPE;
1641 if (RT_FAILURE(RTPollSetQueryHandle(pProcess->hPollSet,
1642 VBOXSERVICECTRLPIPEID_STDOUT, NULL)))
1643 pProcess->hPipeStdOutR = NIL_RTPIPE;
1644 if (RT_FAILURE(RTPollSetQueryHandle(pProcess->hPollSet,
1645 VBOXSERVICECTRLPIPEID_STDIN, NULL)))
1646 pProcess->hPipeStdInW = NIL_RTPIPE;
1647 }
1648 }
1649 RTPollSetDestroy(pProcess->hPollSet);
1650
1651 RTPipeClose(pProcess->hNotificationPipeR);
1652 pProcess->hNotificationPipeR = NIL_RTPIPE;
1653 RTPipeClose(pProcess->hNotificationPipeW);
1654 pProcess->hNotificationPipeW = NIL_RTPIPE;
1655 }
1656 RTPipeClose(pProcess->hPipeStdErrR);
1657 pProcess->hPipeStdErrR = NIL_RTPIPE;
1658 RTHandleClose(phStdErr);
1659 if (phStdErr)
1660 RTHandleClose(phStdErr);
1661 }
1662 RTPipeClose(pProcess->hPipeStdOutR);
1663 pProcess->hPipeStdOutR = NIL_RTPIPE;
1664 RTHandleClose(&hStdOut);
1665 if (phStdOut)
1666 RTHandleClose(phStdOut);
1667 }
1668 RTPipeClose(pProcess->hPipeStdInW);
1669 pProcess->hPipeStdInW = NIL_RTPIPE;
1670 RTHandleClose(phStdIn);
1671 }
1672 }
1673 RTEnvDestroy(hEnv);
1674 }
1675
1676 if (pProcess->uClientID)
1677 {
1678 if (RT_FAILURE(rc))
1679 {
1680 VBGLR3GUESTCTRLCMDCTX ctx = { pProcess->uClientID, pProcess->uContextID };
1681 int rc2 = VbglR3GuestCtrlProcCbStatus(&ctx,
1682 pProcess->uPID, PROC_STS_ERROR, rc,
1683 NULL /* pvData */, 0 /* cbData */);
1684 if ( RT_FAILURE(rc2)
1685 && rc2 != VERR_NOT_FOUND)
1686 VBoxServiceError("[PID %RU32]: Could not report process failure error; rc=%Rrc (process error %Rrc)\n",
1687 pProcess->uPID, rc2, rc);
1688 }
1689
1690 /* Disconnect this client from the guest control service. This also cancels all
1691 * outstanding host requests. */
1692 VBoxServiceVerbose(3, "[PID %RU32]: Disconnecting (client ID=%u) ...\n",
1693 pProcess->uPID, pProcess->uClientID);
1694 VbglR3GuestCtrlDisconnect(pProcess->uClientID);
1695 pProcess->uClientID = 0;
1696 }
1697
1698 /* Free argument + environment variable lists. */
1699 if (uNumEnvVars)
1700 {
1701 for (uint32_t i = 0; i < uNumEnvVars; i++)
1702 RTStrFree(papszEnv[i]);
1703 RTMemFree(papszEnv);
1704 }
1705 if (uNumArgs)
1706 RTGetOptArgvFree(papszArgs);
1707
1708 /*
1709 * If something went wrong signal the user event so that others don't wait
1710 * forever on this thread.
1711 */
1712 if (RT_FAILURE(rc) && !fSignalled)
1713 RTThreadUserSignal(RTThreadSelf());
1714
1715 VBoxServiceVerbose(3, "[PID %RU32]: Thread of process \"%s\" ended with rc=%Rrc\n",
1716 pProcess->uPID, pProcess->StartupInfo.szCmd, rc);
1717
1718 /* Finally, update stopped status. */
1719 ASMAtomicXchgBool(&pProcess->fStopped, true);
1720
1721 return rc;
1722}
1723
1724
1725static int gstcntlProcessLock(PVBOXSERVICECTRLPROCESS pProcess)
1726{
1727 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1728 int rc = RTCritSectEnter(&pProcess->CritSect);
1729 AssertRC(rc);
1730 return rc;
1731}
1732
1733
1734/**
1735 * Thread main routine for a started process.
1736 *
1737 * @return IPRT status code.
1738 * @param RTTHREAD Pointer to the thread's data.
1739 * @param void* User-supplied argument pointer.
1740 *
1741 */
1742static DECLCALLBACK(int) gstcntlProcessThread(RTTHREAD ThreadSelf, void *pvUser)
1743{
1744 PVBOXSERVICECTRLPROCESS pProcess = (VBOXSERVICECTRLPROCESS*)pvUser;
1745 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1746 return gstcntlProcessProcessWorker(pProcess);
1747}
1748
1749
1750static int gstcntlProcessUnlock(PVBOXSERVICECTRLPROCESS pProcess)
1751{
1752 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
1753 int rc = RTCritSectLeave(&pProcess->CritSect);
1754 AssertRC(rc);
1755 return rc;
1756}
1757
1758
1759/**
1760 * Executes (starts) a process on the guest. This causes a new thread to be created
1761 * so that this function will not block the overall program execution.
1762 *
1763 * @return IPRT status code.
1764 * @param pSession Guest session.
1765 * @param pStartupInfo Startup info.
1766 * @param uContextID Context ID to associate the process to start with.
1767
1768 */
1769int GstCntlProcessStart(const PVBOXSERVICECTRLSESSION pSession,
1770 const PVBOXSERVICECTRLPROCSTARTUPINFO pStartupInfo,
1771 uint32_t uContextID)
1772{
1773 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1774 AssertPtrReturn(pStartupInfo, VERR_INVALID_POINTER);
1775
1776 /*
1777 * Allocate new thread data and assign it to our thread list.
1778 */
1779 PVBOXSERVICECTRLPROCESS pProcess = (PVBOXSERVICECTRLPROCESS)RTMemAlloc(sizeof(VBOXSERVICECTRLPROCESS));
1780 if (!pProcess)
1781 return VERR_NO_MEMORY;
1782
1783 int rc = gstcntlProcessInit(pProcess, pSession, pStartupInfo, uContextID);
1784 if (RT_SUCCESS(rc))
1785 {
1786 static uint32_t s_uCtrlExecThread = 0;
1787 if (s_uCtrlExecThread++ == UINT32_MAX)
1788 s_uCtrlExecThread = 0; /* Wrap around to not let IPRT freak out. */
1789 rc = RTThreadCreateF(&pProcess->Thread, gstcntlProcessThread,
1790 pProcess /*pvUser*/, 0 /*cbStack*/,
1791 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "gctl%u", s_uCtrlExecThread);
1792 if (RT_FAILURE(rc))
1793 {
1794 VBoxServiceError("Creating thread for guest process \"%s\" failed: rc=%Rrc, pProcess=%p\n",
1795 pStartupInfo->szCmd, rc, pProcess);
1796
1797 GstCntlProcessFree(pProcess);
1798 }
1799 else
1800 {
1801 VBoxServiceVerbose(4, "Waiting for thread to initialize ...\n");
1802
1803 /* Wait for the thread to initialize. */
1804 rc = RTThreadUserWait(pProcess->Thread, 60 * 1000 /* 60 seconds max. */);
1805 AssertRC(rc);
1806 if ( ASMAtomicReadBool(&pProcess->fShutdown)
1807 || RT_FAILURE(rc))
1808 {
1809 VBoxServiceError("Thread for process \"%s\" failed to start, rc=%Rrc\n",
1810 pStartupInfo->szCmd, rc);
1811
1812 GstCntlProcessFree(pProcess);
1813 }
1814 else
1815 {
1816 ASMAtomicXchgBool(&pProcess->fStarted, true);
1817 }
1818 }
1819 }
1820
1821 return rc;
1822}
1823
1824
1825static DECLCALLBACK(int) gstcntlProcessOnInput(PVBOXSERVICECTRLPROCESS pThis,
1826 const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
1827 bool fPendingClose, void *pvBuf, uint32_t cbBuf)
1828{
1829 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1830 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
1831
1832 int rc;
1833
1834 size_t cbWritten = 0;
1835 if (pvBuf && cbBuf)
1836 {
1837 if (pThis->hPipeStdInW != NIL_RTPIPE)
1838 {
1839 rc = RTPipeWrite(pThis->hPipeStdInW,
1840 pvBuf, cbBuf, &cbWritten);
1841 }
1842 else
1843 rc = VINF_EOF;
1844 }
1845 else
1846 rc = VERR_INVALID_PARAMETER;
1847
1848 /*
1849 * If this is the last write + we have really have written all data
1850 * we need to close the stdin pipe on our end and remove it from
1851 * the poll set.
1852 */
1853 if ( fPendingClose
1854 && (cbBuf == cbWritten))
1855 {
1856 int rc2 = gstcntlProcessPollsetCloseInput(pThis, &pThis->hPipeStdInW);
1857 if (RT_SUCCESS(rc))
1858 rc = rc2;
1859 }
1860
1861 uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status to send back to the host. */
1862 uint32_t uFlags = 0; /* No flags at the moment. */
1863 if (RT_SUCCESS(rc))
1864 {
1865 VBoxServiceVerbose(4, "[PID %RU32]: Written %RU32 bytes input, CID=%RU32, fPendingClose=%RTbool\n",
1866 pThis->uPID, cbWritten, pHostCtx->uContextID, fPendingClose);
1867 uStatus = INPUT_STS_WRITTEN;
1868 }
1869 else
1870 {
1871 if (rc == VERR_BAD_PIPE)
1872 uStatus = INPUT_STS_TERMINATED;
1873 else if (rc == VERR_BUFFER_OVERFLOW)
1874 uStatus = INPUT_STS_OVERFLOW;
1875 /* else undefined */
1876 }
1877
1878 /*
1879 * If there was an error and we did not set the host status
1880 * yet, then do it now.
1881 */
1882 if ( RT_FAILURE(rc)
1883 && uStatus == INPUT_STS_UNDEFINED)
1884 {
1885 uStatus = INPUT_STS_ERROR;
1886 uFlags = rc;
1887 }
1888 Assert(uStatus > INPUT_STS_UNDEFINED);
1889
1890#ifdef DEBUG
1891
1892#endif
1893 int rc2 = VbglR3GuestCtrlProcCbStatusInput(pHostCtx, pThis->uPID,
1894 uStatus, uFlags, (uint32_t)cbWritten);
1895 if (RT_SUCCESS(rc))
1896 rc = rc2;
1897
1898#ifdef DEBUG
1899 VBoxServiceVerbose(3, "[PID %RU32]: gstcntlProcessOnInput returned with rc=%Rrc\n",
1900 pThis->uPID, rc);
1901#endif
1902 return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
1903}
1904
1905
1906static DECLCALLBACK(int) gstcntlProcessOnOutput(PVBOXSERVICECTRLPROCESS pThis,
1907 const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
1908 uint32_t uHandle, uint32_t cbToRead, uint32_t uFlags)
1909{
1910 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
1911 AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
1912
1913 const PVBOXSERVICECTRLSESSION pSession = pThis->pSession;
1914 AssertPtrReturn(pSession, VERR_INVALID_POINTER);
1915
1916 int rc;
1917
1918 uint32_t cbBuf = cbToRead;
1919 uint8_t *pvBuf = (uint8_t *)RTMemAlloc(cbBuf);
1920 if (pvBuf)
1921 {
1922 PRTPIPE phPipe = uHandle == OUTPUT_HANDLE_ID_STDOUT
1923 ? &pThis->hPipeStdOutR
1924 : &pThis->hPipeStdErrR;
1925 AssertPtr(phPipe);
1926
1927 size_t cbRead = 0;
1928 if (*phPipe != NIL_RTPIPE)
1929 {
1930 rc = RTPipeRead(*phPipe, pvBuf, cbBuf, &cbRead);
1931 if (RT_FAILURE(rc))
1932 {
1933 RTPollSetRemove(pThis->hPollSet, uHandle == OUTPUT_HANDLE_ID_STDERR
1934 ? VBOXSERVICECTRLPIPEID_STDERR : VBOXSERVICECTRLPIPEID_STDOUT);
1935 RTPipeClose(*phPipe);
1936 *phPipe = NIL_RTPIPE;
1937 if (rc == VERR_BROKEN_PIPE)
1938 rc = VINF_EOF;
1939 }
1940 }
1941 else
1942 rc = VINF_EOF;
1943
1944#ifdef DEBUG
1945 if (RT_SUCCESS(rc))
1946 {
1947 if ( pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT
1948 && ( uHandle == OUTPUT_HANDLE_ID_STDOUT
1949 || uHandle == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED)
1950 )
1951 {
1952 char szDumpFile[RTPATH_MAX];
1953 if (!RTStrPrintf(szDumpFile, sizeof(szDumpFile), "VBoxService_Session%RU32_PID%RU32_StdOut.txt",
1954 pSession->StartupInfo.uSessionID, pThis->uPID)) rc = VERR_BUFFER_UNDERFLOW;
1955 if (RT_SUCCESS(rc))
1956 rc = gstcntlProcessDumpToFile(szDumpFile, pvBuf, cbRead);
1957 AssertRC(rc);
1958 }
1959 else if ( pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR
1960 && uHandle == OUTPUT_HANDLE_ID_STDERR)
1961 {
1962 char szDumpFile[RTPATH_MAX];
1963 if (!RTStrPrintf(szDumpFile, sizeof(szDumpFile), "VBoxService_Session%RU32_PID%RU32_StdErr.txt",
1964 pSession->StartupInfo.uSessionID, pThis->uPID))
1965 rc = VERR_BUFFER_UNDERFLOW;
1966 if (RT_SUCCESS(rc))
1967 rc = gstcntlProcessDumpToFile(szDumpFile, pvBuf, cbRead);
1968 AssertRC(rc);
1969 }
1970 }
1971#endif
1972
1973 if (RT_SUCCESS(rc))
1974 {
1975#ifdef DEBUG
1976 VBoxServiceVerbose(3, "[PID %RU32]: Read %RU32 bytes output: uHandle=%RU32, CID=%RU32, uFlags=%x\n",
1977 pThis->uPID, cbRead, uHandle, pHostCtx->uContextID, uFlags);
1978#endif
1979 /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
1980 * data which the host needs to work with -- so just pass through all data unfiltered! */
1981
1982 /* Note: Since the context ID is unique the request *has* to be completed here,
1983 * regardless whether we got data or not! Otherwise the waiting events
1984 * on the host never will get completed! */
1985 rc = VbglR3GuestCtrlProcCbOutput(pHostCtx, pThis->uPID, uHandle, uFlags,
1986 pvBuf, cbRead);
1987 if ( RT_FAILURE(rc)
1988 && rc == VERR_NOT_FOUND) /* Not critical if guest PID is not found on the host (anymore). */
1989 rc = VINF_SUCCESS;
1990 }
1991
1992 RTMemFree(pvBuf);
1993 }
1994 else
1995 rc = VERR_NO_MEMORY;
1996
1997#ifdef DEBUG
1998 VBoxServiceVerbose(3, "[PID %RU32]: Reading output returned with rc=%Rrc\n",
1999 pThis->uPID, rc);
2000#endif
2001 return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
2002}
2003
2004
2005static DECLCALLBACK(int) gstcntlProcessOnTerm(PVBOXSERVICECTRLPROCESS pThis)
2006{
2007 AssertPtrReturn(pThis, VERR_INVALID_POINTER);
2008
2009 if (!ASMAtomicXchgBool(&pThis->fShutdown, true))
2010 {
2011 VBoxServiceVerbose(3, "[PID %RU32]: Setting shutdown flag ...\n",
2012 pThis->uPID);
2013 }
2014
2015 return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
2016}
2017
2018
2019int gstcntlProcessRequestExV(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
2020 bool fAsync, RTMSINTERVAL uTimeoutMS, PRTREQ pReq, PFNRT pfnFunction,
2021 unsigned cArgs, va_list Args)
2022{
2023 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
2024 /* pHostCtx is optional. */
2025 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
2026 if (!fAsync)
2027 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
2028
2029 int rc = gstcntlProcessLock(pProcess);
2030 if (RT_SUCCESS(rc))
2031 {
2032#ifdef DEBUG
2033 VBoxServiceVerbose(3, "[PID %RU32]: gstcntlProcessRequestExV fAsync=%RTbool, uTimeoutMS=%RU32, cArgs=%u\n",
2034 pProcess->uPID, fAsync, uTimeoutMS, cArgs);
2035#endif
2036 uint32_t uFlags = RTREQFLAGS_IPRT_STATUS;
2037 if (fAsync)
2038 {
2039 Assert(uTimeoutMS == 0);
2040 uFlags |= RTREQFLAGS_NO_WAIT;
2041 }
2042
2043 rc = RTReqQueueCallV(pProcess->hReqQueue, &pReq, uTimeoutMS, uFlags,
2044 pfnFunction, cArgs, Args);
2045 if (RT_SUCCESS(rc))
2046 {
2047 /* Wake up the process' notification pipe to get
2048 * the request being processed. */
2049 Assert(pProcess->hNotificationPipeW != NIL_RTPIPE);
2050 size_t cbWritten = 0;
2051 rc = RTPipeWrite(pProcess->hNotificationPipeW, "i", 1, &cbWritten);
2052 if ( RT_SUCCESS(rc)
2053 && cbWritten != 1)
2054 {
2055 VBoxServiceError("[PID %RU32]: Notification pipe got %zu bytes instead of 1\n",
2056 pProcess->uPID, cbWritten);
2057 }
2058 else if (RT_UNLIKELY(RT_FAILURE(rc)))
2059 VBoxServiceError("[PID %RU32]: Writing to notification pipe failed, rc=%Rrc\n",
2060 pProcess->uPID, rc);
2061 }
2062 else
2063 VBoxServiceError("[PID %RU32]: RTReqQueueCallV failed, rc=%Rrc\n",
2064 pProcess->uPID, rc);
2065
2066 int rc2 = gstcntlProcessUnlock(pProcess);
2067 if (RT_SUCCESS(rc))
2068 rc = rc2;
2069 }
2070
2071#ifdef DEBUG
2072 VBoxServiceVerbose(3, "[PID %RU32]: gstcntlProcessRequestExV returned rc=%Rrc\n",
2073 pProcess->uPID, rc);
2074#endif
2075 return rc;
2076}
2077
2078
2079int gstcntlProcessRequestAsync(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
2080 PFNRT pfnFunction, unsigned cArgs, ...)
2081{
2082 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
2083 /* pHostCtx is optional. */
2084 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
2085
2086 va_list va;
2087 va_start(va, cArgs);
2088 int rc = gstcntlProcessRequestExV(pProcess, pHostCtx, true /* fAsync */, 0 /* uTimeoutMS */,
2089 NULL /* pReq */, pfnFunction, cArgs, va);
2090 va_end(va);
2091
2092 return rc;
2093}
2094
2095
2096int gstcntlProcessRequestWait(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
2097 RTMSINTERVAL uTimeoutMS, PRTREQ pReq, PFNRT pfnFunction, unsigned cArgs, ...)
2098{
2099 AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
2100 /* pHostCtx is optional. */
2101 AssertPtrReturn(pfnFunction, VERR_INVALID_POINTER);
2102
2103 va_list va;
2104 va_start(va, cArgs);
2105 int rc = gstcntlProcessRequestExV(pProcess, pHostCtx, false /* fAsync */, uTimeoutMS,
2106 pReq, pfnFunction, cArgs, va);
2107 va_end(va);
2108
2109 return rc;
2110}
2111
2112
2113int GstCntlProcessHandleInput(PVBOXSERVICECTRLPROCESS pProcess, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
2114 bool fPendingClose, void *pvBuf, uint32_t cbBuf)
2115{
2116 if (!ASMAtomicReadBool(&pProcess->fShutdown))
2117 return gstcntlProcessRequestAsync(pProcess, pHostCtx, (PFNRT)gstcntlProcessOnInput,
2118 5 /* cArgs */, pProcess, pHostCtx, fPendingClose, pvBuf, cbBuf);
2119
2120 return gstcntlProcessOnInput(pProcess, pHostCtx, fPendingClose, pvBuf, cbBuf);
2121}
2122
2123
2124int GstCntlProcessHandleOutput(PVBOXSERVICECTRLPROCESS pProcess, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
2125 uint32_t uHandle, uint32_t cbToRead, uint32_t uFlags)
2126{
2127 if (!ASMAtomicReadBool(&pProcess->fShutdown))
2128 return gstcntlProcessRequestAsync(pProcess, pHostCtx, (PFNRT)gstcntlProcessOnOutput,
2129 5 /* cArgs */, pProcess, pHostCtx, uHandle, cbToRead, uFlags);
2130
2131 return gstcntlProcessOnOutput(pProcess, pHostCtx, uHandle, cbToRead, uFlags);
2132}
2133
2134
2135int GstCntlProcessHandleTerm(PVBOXSERVICECTRLPROCESS pProcess)
2136{
2137 if (!ASMAtomicReadBool(&pProcess->fShutdown))
2138 return gstcntlProcessRequestAsync(pProcess, NULL /* pHostCtx */, (PFNRT)gstcntlProcessOnTerm,
2139 1 /* cArgs */, pProcess);
2140
2141 return gstcntlProcessOnTerm(pProcess);
2142}
2143
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