VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlExec.cpp@ 36733

Last change on this file since 36733 was 36733, checked in by vboxsync, 14 years ago

VBoxService/GuestCtrl: Fix argv[0] handling when executing guest programs.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 54.1 KB
Line 
1/* $Id: VBoxServiceControlExec.cpp 36733 2011-04-19 16:03:27Z vboxsync $ */
2/** @file
3 * VBoxServiceControlExec - Utility functions for process execution.
4 */
5
6/*
7 * Copyright (C) 2011 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/assert.h>
23#include <iprt/crc.h>
24#include <iprt/ctype.h>
25#include <iprt/env.h>
26#include <iprt/file.h>
27#include <iprt/getopt.h>
28#include <iprt/handle.h>
29#include <iprt/mem.h>
30#include <iprt/path.h>
31#include <iprt/param.h>
32#include <iprt/pipe.h>
33#include <iprt/poll.h>
34#include <iprt/process.h>
35#include <iprt/string.h>
36#include <iprt/stream.h>
37#include <iprt/thread.h>
38#include <VBox/version.h>
39#include <VBox/VBoxGuestLib.h>
40#include <VBox/HostServices/GuestControlSvc.h>
41
42#include "VBoxServiceInternal.h"
43#include "VBoxServiceUtils.h"
44#include "VBoxServicePipeBuf.h"
45#include "VBoxServiceControlExecThread.h"
46
47using namespace guestControl;
48
49extern RTLISTNODE g_GuestControlExecThreads;
50extern RTCRITSECT g_GuestControlExecThreadsCritSect;
51
52
53/**
54 * Handle an error event on standard input.
55 *
56 * @returns IPRT status code.
57 * @param hPollSet The polling set.
58 * @param fPollEvt The event mask returned by RTPollNoResume.
59 * @param phStdInW The standard input pipe handle.
60 * @param pStdInBuf The standard input buffer.
61 */
62static int VBoxServiceControlExecProcHandleStdInErrorEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
63 PVBOXSERVICECTRLEXECPIPEBUF pStdInBuf)
64{
65 int rc = RTPollSetRemove(hPollSet, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE);
66 /* Don't assert if writable handle is not in poll set anymore. */
67 if ( RT_FAILURE(rc)
68 && rc != VERR_POLL_HANDLE_ID_NOT_FOUND)
69 {
70 AssertRC(rc);
71 }
72
73 /* Close writable stdin pipe. */
74 rc = RTPipeClose(*phStdInW);
75 AssertRC(rc);
76 *phStdInW = NIL_RTPIPE;
77
78 /* Mark the stdin buffer as dead; we're not using it anymore. */
79 rc = VBoxServicePipeBufSetStatus(pStdInBuf, false /* Disabled */);
80 AssertRC(rc);
81
82 /* Remove stdin error handle from set. */
83 rc = RTPollSetRemove(hPollSet, VBOXSERVICECTRLPIPEID_STDIN_ERROR);
84 /* Don't assert if writable handle is not in poll set anymore. */
85 if ( RT_FAILURE(rc)
86 && rc != VERR_POLL_HANDLE_ID_NOT_FOUND)
87 {
88 AssertRC(rc);
89 }
90 else
91 rc = VINF_SUCCESS;
92
93 return rc;
94}
95
96
97/**
98 * Try write some more data to the standard input of the child.
99 *
100 * @returns IPRT status code.
101 * @retval VINF_TRY_AGAIN if there is still data left in the buffer.
102 *
103 * @param hPollSet The polling set.
104 * @param pStdInBuf The standard input buffer.
105 * @param hStdInW The standard input pipe.
106 * @param pfClose Pointer to a flag whether the pipe needs to be closed afterwards.
107 */
108static int VBoxServiceControlExecProcWriteStdIn(RTPOLLSET hPollSet, PVBOXSERVICECTRLEXECPIPEBUF pStdInBuf, RTPIPE hStdInW,
109 size_t *pcbWritten, bool *pfClose)
110{
111 AssertPtrReturn(pStdInBuf, VERR_INVALID_PARAMETER);
112 AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
113 AssertPtrReturn(pfClose, VERR_INVALID_PARAMETER);
114
115 size_t cbLeft;
116 int rc = VBoxServicePipeBufWriteToPipe(pStdInBuf, hStdInW, pcbWritten, &cbLeft);
117
118 /* If we have written all data which is in the buffer set the close flag. */
119 *pfClose = (cbLeft == 0) && VBoxServicePipeBufIsClosing(pStdInBuf);
120
121 if ( !*pcbWritten
122 && VBoxServicePipeBufIsEnabled(pStdInBuf))
123 {
124 /*
125 * Nothing else left to write now? Remove the writable event from the poll set
126 * to not trigger too high CPU loads.
127 */
128 int rc2 = RTPollSetRemove(hPollSet, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE);
129 AssertRC(rc2);
130 }
131
132 VBoxServiceVerbose(3, "VBoxServiceControlExecProcWriteStdIn: Written=%u, Left=%u, rc=%Rrc\n",
133 *pcbWritten, cbLeft, rc);
134 return rc;
135}
136
137
138/**
139 * Handle an event indicating we can write to the standard input pipe of the
140 * child process.
141 *
142 * @returns IPRT status code.
143 * @param hPollSet The polling set.
144 * @param fPollEvt The event mask returned by RTPollNoResume.
145 * @param phStdInW The standard input pipe.
146 * @param pStdInBuf The standard input buffer.
147 * @param pcbWritten Where to return the number of bytes written.
148 */
149static int VBoxServiceControlExecProcHandleStdInWritableEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phStdInW,
150 PVBOXSERVICECTRLEXECPIPEBUF pStdInBuf, size_t *pcbWritten)
151{
152 AssertPtrReturn(pcbWritten, VERR_INVALID_PARAMETER);
153 int rc;
154 if (!(fPollEvt & RTPOLL_EVT_ERROR))
155 {
156 bool fClose;
157 rc = VBoxServiceControlExecProcWriteStdIn(hPollSet,
158 pStdInBuf, *phStdInW,
159 pcbWritten, &fClose);
160 if ( rc == VINF_TRY_AGAIN
161 || rc == VERR_MORE_DATA)
162 rc = VINF_SUCCESS;
163 if (RT_FAILURE(rc))
164 {
165 if ( rc == VERR_BAD_PIPE
166 || rc == VERR_BROKEN_PIPE)
167 {
168 rc = RTPollSetRemove(hPollSet, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE);
169 AssertRC(rc);
170 }
171 else
172 {
173 /** @todo Do we need to do something about this error condition? */
174 AssertRC(rc);
175 }
176 }
177 else if (fClose)
178 {
179 /* If the pipe needs to be closed, do so. */
180 rc = VBoxServiceControlExecProcHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf);
181 }
182 }
183 else
184 {
185 *pcbWritten = 0;
186 rc = VBoxServiceControlExecProcHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, pStdInBuf);
187 }
188 return rc;
189}
190
191
192/**
193 * Handle pending output data or error on standard out, standard error or the
194 * test pipe.
195 *
196 * @returns IPRT status code from client send.
197 * @param pThread The thread specific data.
198 * @param hPollSet The polling set.
199 * @param fPollEvt The event mask returned by RTPollNoResume.
200 * @param phPipeR The pipe handle.
201 * @param pu32Crc The current CRC-32 of the stream. (In/Out)
202 * @param uHandleId The handle ID.
203 *
204 * @todo Put the last 4 parameters into a struct!
205 */
206static int VBoxServiceControlExecProcHandleOutputEvent(RTPOLLSET hPollSet, uint32_t fPollEvt, PRTPIPE phPipeR,
207 uint32_t uHandleId, PVBOXSERVICECTRLEXECPIPEBUF pStdOutBuf)
208{
209#ifdef DEBUG
210 VBoxServiceVerbose(4, "ControlExec: HandleOutputEvent: fPollEvt=%#x\n", fPollEvt);
211#endif
212
213 /*
214 * Try drain the pipe before acting on any errors.
215 */
216 int rc = VINF_SUCCESS;
217 size_t cbRead;
218 uint8_t abBuf[_64K];
219
220 int rc2 = RTPipeRead(*phPipeR, abBuf, sizeof(abBuf), &cbRead);
221 if (RT_SUCCESS(rc2) && cbRead)
222 {
223 uint32_t cbWritten;
224 rc = VBoxServicePipeBufWriteToBuf(pStdOutBuf, abBuf,
225 cbRead, false /* Pending close */, &cbWritten);
226 if (RT_SUCCESS(rc))
227 {
228 Assert(cbRead == cbWritten);
229 /* Make sure we go another poll round in case there was too much data
230 for the buffer to hold. */
231 fPollEvt &= RTPOLL_EVT_ERROR;
232 }
233 }
234 else if (RT_FAILURE(rc2))
235 {
236 fPollEvt |= RTPOLL_EVT_ERROR;
237 AssertMsg(rc2 == VERR_BROKEN_PIPE, ("%Rrc\n", rc));
238 }
239
240 /*
241 * If an error was signalled, close reading stdout/stderr pipe.
242 */
243 if (fPollEvt & RTPOLL_EVT_ERROR)
244 {
245 rc2 = RTPollSetRemove(hPollSet, uHandleId);
246 AssertRC(rc2);
247
248 rc2 = RTPipeClose(*phPipeR);
249 AssertRC(rc2);
250 *phPipeR = NIL_RTPIPE;
251 }
252 return rc;
253}
254
255
256int VBoxServiceControlExecProcHandleStdInputNotify(RTPOLLSET hPollSet,
257 PRTPIPE phNotificationPipeR, PRTPIPE phInputPipeW)
258{
259#ifdef DEBUG
260 VBoxServiceVerbose(4, "ControlExec: HandleStdInputNotify\n");
261#endif
262 /* Drain the notification pipe. */
263 uint8_t abBuf[8];
264 size_t cbIgnore;
265 int rc = RTPipeRead(*phNotificationPipeR, abBuf, sizeof(abBuf), &cbIgnore);
266 if (RT_SUCCESS(rc))
267 {
268 /*
269 * When the writable handle previously was removed from the poll set we need to add
270 * it here again so that writable events from the started procecss get handled correctly.
271 */
272 RTHANDLE hWritableIgnored;
273 rc = RTPollSetQueryHandle(hPollSet, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE, &hWritableIgnored);
274 if (rc == VERR_POLL_HANDLE_ID_NOT_FOUND)
275 rc = RTPollSetAddPipe(hPollSet, *phInputPipeW, RTPOLL_EVT_WRITE, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE);
276 }
277 return rc;
278}
279
280
281/**
282 * Execution loop which runs in a dedicated per-started-process thread and
283 * handles all pipe input/output and signalling stuff.
284 *
285 * @return IPRT status code.
286 * @param pThread The process' thread handle.
287 * @param hProcess The actual process handle.
288 * @param cMsTimeout Time limit (in ms) of the process' life time.
289 * @param hPollSet The poll set to use.
290 * @param hStdInW Handle to the process' stdin write end.
291 * @param hStdOutR Handle to the process' stdout read end.
292 * @param hStdErrR Handle to the process' stderr read end.
293 */
294static int VBoxServiceControlExecProcLoop(PVBOXSERVICECTRLTHREAD pThread,
295 RTPROCESS hProcess, RTMSINTERVAL cMsTimeout, RTPOLLSET hPollSet,
296 PRTPIPE phStdInW, PRTPIPE phStdOutR, PRTPIPE phStdErrR)
297{
298 AssertPtrReturn(phStdInW, VERR_INVALID_PARAMETER);
299 AssertPtrReturn(phStdOutR, VERR_INVALID_PARAMETER);
300 AssertPtrReturn(phStdErrR, VERR_INVALID_PARAMETER);
301
302 int rc;
303 int rc2;
304 uint64_t const MsStart = RTTimeMilliTS();
305 RTPROCSTATUS ProcessStatus = { 254, RTPROCEXITREASON_ABEND };
306 bool fProcessAlive = true;
307 bool fProcessTimedOut = false;
308 uint64_t MsProcessKilled = UINT64_MAX;
309 RTMSINTERVAL const cMsPollBase = *phStdInW != NIL_RTPIPE
310 ? 100 /* Need to poll for input. */
311 : 1000; /* Need only poll for process exit and aborts. */
312 RTMSINTERVAL cMsPollCur = 0;
313
314 AssertPtr(pThread);
315 Assert(pThread->enmType == kVBoxServiceCtrlThreadDataExec);
316 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pThread->pvData;
317 AssertPtr(pData);
318
319 /* Assign PID to thread data. */
320 pData->uPID = hProcess;
321
322 /*
323 * Before entering the loop, tell the host that we've started the guest
324 * and that it's now OK to send input to the process.
325 */
326 VBoxServiceVerbose(3, "ControlExec: Process started: PID=%u, CID=%u, User=%s\n",
327 pData->uPID, pThread->uContextID, pData->pszUser);
328 rc = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID,
329 pData->uPID, PROC_STS_STARTED, 0 /* u32Flags */,
330 NULL /* pvData */, 0 /* cbData */);
331
332 /*
333 * Process input, output, the test pipe and client requests.
334 */
335 while ( RT_SUCCESS(rc)
336 && RT_UNLIKELY(!pThread->fShutdown))
337 {
338 /*
339 * Wait/Process all pending events.
340 */
341 uint32_t idPollHnd;
342 uint32_t fPollEvt;
343 rc2 = RTPollNoResume(hPollSet, cMsPollCur, &fPollEvt, &idPollHnd);
344 if (pThread->fShutdown)
345 continue;
346
347 cMsPollCur = 0; /* No rest until we've checked everything. */
348
349 if (RT_SUCCESS(rc2))
350 {
351 VBoxServiceVerbose(4, "ControlExec: RTPollNoResume idPollHnd=%u\n", idPollHnd);
352 switch (idPollHnd)
353 {
354 case VBOXSERVICECTRLPIPEID_STDIN_ERROR:
355 rc = VBoxServiceControlExecProcHandleStdInErrorEvent(hPollSet, fPollEvt, phStdInW, &pData->stdIn);
356 break;
357
358 case VBOXSERVICECTRLPIPEID_STDIN_INPUT_NOTIFY:
359 rc = VBoxServiceControlExecProcHandleStdInputNotify(hPollSet,
360 &pData->stdIn.hNotificationPipeR, &pData->pipeStdInW);
361 AssertRC(rc);
362 /* Fall through. */
363 case VBOXSERVICECTRLPIPEID_STDIN_WRITABLE:
364 {
365 size_t cbWritten;
366 rc = VBoxServiceControlExecProcHandleStdInWritableEvent(hPollSet, fPollEvt, phStdInW,
367 &pData->stdIn, &cbWritten);
368 break;
369 }
370
371 case VBOXSERVICECTRLPIPEID_STDOUT:
372 rc = VBoxServiceControlExecProcHandleOutputEvent(hPollSet, fPollEvt, phStdOutR,
373 VBOXSERVICECTRLPIPEID_STDOUT, &pData->stdOut);
374 break;
375
376 case VBOXSERVICECTRLPIPEID_STDERR:
377 rc = VBoxServiceControlExecProcHandleOutputEvent(hPollSet, fPollEvt, phStdErrR,
378 VBOXSERVICECTRLPIPEID_STDERR, &pData->stdOut);
379 break;
380
381 default:
382 AssertMsgFailed(("idPollHnd=%u fPollEvt=%#x\n", idPollHnd, fPollEvt));
383 break;
384 }
385 if (RT_FAILURE(rc) || rc == VINF_EOF)
386 break; /* Abort command, or client dead or something. */
387 continue;
388 }
389
390 /*
391 * Check for process death.
392 */
393 if (fProcessAlive)
394 {
395 rc2 = RTProcWaitNoResume(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
396 if (RT_SUCCESS_NP(rc2))
397 {
398 fProcessAlive = false;
399 continue;
400 }
401 if (RT_UNLIKELY(rc2 == VERR_INTERRUPTED))
402 continue;
403 if (RT_UNLIKELY(rc2 == VERR_PROCESS_NOT_FOUND))
404 {
405 fProcessAlive = false;
406 ProcessStatus.enmReason = RTPROCEXITREASON_ABEND;
407 ProcessStatus.iStatus = 255;
408 AssertFailed();
409 }
410 else
411 AssertMsg(rc2 == VERR_PROCESS_RUNNING, ("%Rrc\n", rc2));
412 }
413
414 /*
415 * If the process has terminated, we're should head out.
416 */
417 if (!fProcessAlive)
418 break;
419
420 /*
421 * Check for timed out, killing the process.
422 */
423 uint32_t cMilliesLeft = RT_INDEFINITE_WAIT;
424 if (cMsTimeout != RT_INDEFINITE_WAIT)
425 {
426 uint64_t u64Now = RTTimeMilliTS();
427 uint64_t cMsElapsed = u64Now - MsStart;
428 if (cMsElapsed >= cMsTimeout)
429 {
430 VBoxServiceVerbose(3, "ControlExec: Process timed out (%ums elapsed > %ums timeout), killing ...", cMsElapsed, cMsTimeout);
431
432 fProcessTimedOut = true;
433 if ( MsProcessKilled == UINT64_MAX
434 || u64Now - MsProcessKilled > 1000)
435 {
436 if (u64Now - MsProcessKilled > 20*60*1000)
437 break; /* Give up after 20 mins. */
438 RTProcTerminate(hProcess);
439 MsProcessKilled = u64Now;
440 continue;
441 }
442 cMilliesLeft = 10000;
443 }
444 else
445 cMilliesLeft = cMsTimeout - (uint32_t)cMsElapsed;
446 }
447
448 /* Reset the polling interval since we've done all pending work. */
449 cMsPollCur = cMilliesLeft >= cMsPollBase ? cMsPollBase : cMilliesLeft;
450
451 /*
452 * Need to exit?
453 */
454 if (pThread->fShutdown)
455 break;
456 }
457
458 /*
459 * Try kill the process if it's still alive at this point.
460 */
461 if (fProcessAlive)
462 {
463 if (MsProcessKilled == UINT64_MAX)
464 {
465 VBoxServiceVerbose(3, "ControlExec: Process (PID=%u) is still alive and not killed yet\n",
466 pData->uPID);
467
468 MsProcessKilled = RTTimeMilliTS();
469 RTProcTerminate(hProcess);
470 RTThreadSleep(500);
471 }
472
473 for (size_t i = 0; i < 10; i++)
474 {
475 VBoxServiceVerbose(4, "ControlExec: Kill attempt %d/10: Waiting for process (PID=%u) exit ...\n",
476 i + 1, pData->uPID);
477 rc2 = RTProcWait(hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
478 if (RT_SUCCESS(rc2))
479 {
480 VBoxServiceVerbose(4, "ControlExec: Kill attempt %d/10: Process (PID=%u) exited\n",
481 i + 1, pData->uPID);
482 fProcessAlive = false;
483 break;
484 }
485 if (i >= 5)
486 {
487 VBoxServiceVerbose(4, "ControlExec: Kill attempt %d/10: Try to terminate (PID=%u) ...\n",
488 i + 1, pData->uPID);
489 RTProcTerminate(hProcess);
490 }
491 RTThreadSleep(i >= 5 ? 2000 : 500);
492 }
493
494 if (fProcessAlive)
495 VBoxServiceVerbose(3, "ControlExec: Process (PID=%u) could not be killed\n", pData->uPID);
496 }
497
498 /*
499 * If we don't have a client problem (RT_FAILURE(rc) we'll reply to the
500 * clients exec packet now.
501 */
502 if (RT_SUCCESS(rc))
503 {
504 VBoxServicePipeBufSetStatus(&pData->stdIn, false /* Disabled */);
505 VBoxServicePipeBufSetStatus(&pData->stdOut, false /* Disabled */);
506 VBoxServicePipeBufSetStatus(&pData->stdErr, false /* Disabled */);
507
508 /* Since the process is not alive anymore, destroy its local
509 * stdin pipe buffer - it's not used anymore and can eat up quite
510 * a bit of memory. */
511 VBoxServicePipeBufDestroy(&pData->stdIn);
512
513 uint32_t uStatus = PROC_STS_UNDEFINED;
514 uint32_t uFlags = 0;
515
516 if ( fProcessTimedOut && !fProcessAlive && MsProcessKilled != UINT64_MAX)
517 {
518 VBoxServiceVerbose(3, "ControlExec: Process timed out and got killed\n");
519 uStatus = PROC_STS_TOK;
520 }
521 else if (fProcessTimedOut && fProcessAlive && MsProcessKilled != UINT64_MAX)
522 {
523 VBoxServiceVerbose(3, "ControlExec: Process timed out and did *not* get killed\n");
524 uStatus = PROC_STS_TOA;
525 }
526 else if (pThread->fShutdown && (fProcessAlive || MsProcessKilled != UINT64_MAX))
527 {
528 VBoxServiceVerbose(3, "ControlExec: Process got terminated because system/service is about to shutdown\n");
529 uStatus = PROC_STS_DWN; /* Service is stopping, process was killed. */
530 uFlags = pData->uFlags; /* Return handed-in execution flags back to the host. */
531 }
532 else if (fProcessAlive)
533 {
534 VBoxServiceError("ControlExec: Process is alive when it should not!\n");
535 }
536 else if (MsProcessKilled != UINT64_MAX)
537 {
538 VBoxServiceError("ControlExec: Process has been killed when it should not!\n");
539 }
540 else if (ProcessStatus.enmReason == RTPROCEXITREASON_NORMAL)
541 {
542 VBoxServiceVerbose(3, "ControlExec: Process ended with RTPROCEXITREASON_NORMAL (%u)\n",
543 ProcessStatus.iStatus);
544
545 uStatus = PROC_STS_TEN;
546 uFlags = ProcessStatus.iStatus;
547 }
548 else if (ProcessStatus.enmReason == RTPROCEXITREASON_SIGNAL)
549 {
550 VBoxServiceVerbose(3, "ControlExec: Process ended with RTPROCEXITREASON_SIGNAL (%u)\n",
551 ProcessStatus.iStatus);
552
553 uStatus = PROC_STS_TES;
554 uFlags = ProcessStatus.iStatus;
555 }
556 else if (ProcessStatus.enmReason == RTPROCEXITREASON_ABEND)
557 {
558 VBoxServiceVerbose(3, "ControlExec: Process ended with RTPROCEXITREASON_ABEND (%u)\n",
559 ProcessStatus.iStatus);
560
561 uStatus = PROC_STS_TEA;
562 uFlags = ProcessStatus.iStatus;
563 }
564 else
565 {
566 VBoxServiceError("ControlExec: Process has reached an undefined status!\n");
567 }
568
569 VBoxServiceVerbose(3, "ControlExec: Process ended: PID=%u, CID=%u, Status=%u, Flags=%u\n",
570 pData->uPID, pThread->uContextID, uStatus, uFlags);
571 rc = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID,
572 pData->uPID, uStatus, uFlags,
573 NULL /* pvData */, 0 /* cbData */);
574 VBoxServiceVerbose(3, "ControlExec: Process loop ended with rc=%Rrc\n", rc);
575 }
576 else
577 VBoxServiceError("ControlExec: Process loop failed with rc=%Rrc\n", rc);
578 return rc;
579}
580
581
582/**
583 * Sets up the redirection / pipe / nothing for one of the standard handles.
584 *
585 * @returns IPRT status code. No client replies made.
586 * @param fd Which standard handle it is (0 == stdin, 1 ==
587 * stdout, 2 == stderr).
588 * @param ph The generic handle that @a pph may be set
589 * pointing to. Always set.
590 * @param pph Pointer to the RTProcCreateExec argument.
591 * Always set.
592 * @param phPipe Where to return the end of the pipe that we
593 * should service. Always set.
594 */
595static int VBoxServiceControlExecSetupPipe(int fd, PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe)
596{
597 AssertPtrReturn(ph, VERR_INVALID_PARAMETER);
598 AssertPtrReturn(pph, VERR_INVALID_PARAMETER);
599 AssertPtrReturn(phPipe, VERR_INVALID_PARAMETER);
600
601 ph->enmType = RTHANDLETYPE_PIPE;
602 ph->u.hPipe = NIL_RTPIPE;
603 *pph = NULL;
604 *phPipe = NIL_RTPIPE;
605
606 int rc;
607
608 /*
609 * Setup a pipe for forwarding to/from the client.
610 * The ph union struct will be filled with a pipe read/write handle
611 * to represent the "other" end to phPipe.
612 */
613 if (fd == 0) /* stdin? */
614 {
615 /* Connect a wrtie pipe specified by phPipe to stdin. */
616 rc = RTPipeCreate(&ph->u.hPipe, phPipe, RTPIPE_C_INHERIT_READ);
617 }
618 else /* stdout or stderr? */
619 {
620 /* Connect a read pipe specified by phPipe to stdout or stderr. */
621 rc = RTPipeCreate(phPipe, &ph->u.hPipe, RTPIPE_C_INHERIT_WRITE);
622 }
623 if (RT_FAILURE(rc))
624 return rc;
625 ph->enmType = RTHANDLETYPE_PIPE;
626 *pph = ph;
627
628 return rc;
629}
630
631
632/**
633 * Expands a file name / path to its real content. This only works on Windows
634 * for now (e.g. translating "%TEMP%\foo.exe" to "C:\Windows\Temp" when starting
635 * with system / administrative rights).
636 *
637 * @return IPRT status code.
638 * @param pszPath Path to resolve.
639 * @param pszExpanded Pointer to string to store the resolved path in.
640 * @param cbExpanded Size (in bytes) of string to store the resolved path.
641 */
642static int VBoxServiceControlExecMakeFullPath(const char *pszPath, char *pszExpanded, size_t cbExpanded)
643{
644 int rc = VINF_SUCCESS;
645#ifdef RT_OS_WINDOWS
646 if (!ExpandEnvironmentStrings(pszPath, pszExpanded, cbExpanded))
647 rc = RTErrConvertFromWin32(GetLastError());
648#else
649 /* No expansion for non-Windows yet. */
650 rc = RTStrCopy(pszExpanded, cbExpanded, pszPath);
651#endif
652#ifdef DEBUG
653 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecMakeFullPath: %s -> %s\n",
654 pszPath, pszExpanded);
655#endif
656 return rc;
657}
658
659
660/**
661 * Resolves the full path of a specified executable name. This function also
662 * resolves internal VBoxService tools to its appropriate executable path + name.
663 *
664 * @return IPRT status code.
665 * @param pszFileName File name to resovle.
666 * @param pszResolved Pointer to a string where the resolved file name will be stored.
667 * @param cbResolved Size (in bytes) of resolved file name string.
668 */
669static int VBoxServiceControlExecResolveExecutable(const char *pszFileName, char *pszResolved, size_t cbResolved)
670{
671 int rc = VINF_SUCCESS;
672
673 /* Search the path of our executable. */
674 char szVBoxService[RTPATH_MAX];
675 if (RTProcGetExecutablePath(szVBoxService, sizeof(szVBoxService)))
676 {
677 char *pszExecResolved = NULL;
678 if ( (g_pszProgName && RTStrICmp(pszFileName, g_pszProgName) == 0)
679 || !RTStrICmp(pszFileName, VBOXSERVICE_NAME))
680 {
681 /* We just want to execute VBoxService (no toolbox). */
682 pszExecResolved = RTStrDup(szVBoxService);
683 }
684#ifdef VBOXSERVICE_TOOLBOX
685 else if (RTStrStr(pszFileName, "vbox_") == pszFileName)
686 {
687 /* We want to use the internal toolbox (all internal
688 * tools are starting with "vbox_" (e.g. "vbox_cat"). */
689 pszExecResolved = RTStrDup(szVBoxService);
690 }
691#endif
692 else /* Nothing to resolve, copy original. */
693 pszExecResolved = RTStrDup(pszFileName);
694 AssertPtr(pszExecResolved);
695
696 rc = VBoxServiceControlExecMakeFullPath(pszExecResolved, pszResolved, cbResolved);
697#ifdef DEBUG
698 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecResolveExecutable: %s -> %s\n",
699 pszFileName, pszResolved);
700#endif
701 RTStrFree(pszExecResolved);
702 }
703 return rc;
704}
705
706
707#ifdef VBOXSERVICE_TOOLBOX
708/**
709 * Constructs the argv command line by resolving environment variables
710 * and relative paths.
711 *
712 * @return IPRT status code.
713 * @param pszArgv0 First argument (argv0), either original or modified version.
714 * @param papszArgs Original argv command line from the host, starting at argv[1].
715 * @param ppapszArgv Pointer to a pointer with the new argv command line.
716 * Needs to be freed with RTGetOptArgvFree.
717 */
718static int VBoxServiceControlExecPrepareArgv(const char *pszArgv0,
719 const char * const *papszArgs, char ***ppapszArgv)
720{
721 AssertPtrReturn(pszArgv0, VERR_INVALID_PARAMETER);
722 AssertPtrReturn(ppapszArgv, VERR_INVALID_PARAMETER);
723
724/** @todo RTGetOptArgvToString converts to MSC quoted string, while
725 * RTGetOptArgvFromString takes bourne shell according to the docs...
726 * Actually, converting to and from here is a very roundabout way of prepending
727 * an entry (pszFilename) to an array (*ppapszArgv). */
728 char *pszArgs;
729 int rc = RTGetOptArgvToString(&pszArgs, papszArgs,
730 RTGETOPTARGV_CNV_QUOTE_MS_CRT); /* RTGETOPTARGV_CNV_QUOTE_BOURNE_SH */
731 if ( RT_SUCCESS(rc)
732 && *pszArgs)
733 {
734 /*
735 * Construct the new command line by appending the actual
736 * tool name to new process' command line.
737 */
738 char szArgsExp[RTPATH_MAX];
739 rc = VBoxServiceControlExecMakeFullPath(pszArgs, szArgsExp, sizeof(szArgsExp));
740 if (RT_SUCCESS(rc))
741 {
742 char *pszNewArgs;
743 if (RTStrAPrintf(&pszNewArgs, "%s %s", pszArgv0, szArgsExp))
744 {
745#ifdef DEBUG
746 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecPrepareArgv: %s\n",
747 pszNewArgs);
748#endif
749 int iNumArgsIgnored;
750 rc = RTGetOptArgvFromString(ppapszArgv, &iNumArgsIgnored,
751 pszNewArgs, NULL /* Use standard separators. */);
752 RTStrFree(pszNewArgs);
753 }
754 }
755 RTStrFree(pszArgs);
756 }
757 else /* No arguments given, just use the resolved file name as argv[0]. */
758 {
759 int iNumArgsIgnored;
760 rc = RTGetOptArgvFromString(ppapszArgv, &iNumArgsIgnored,
761 pszArgv0, NULL /* Use standard separators. */);
762 }
763 return rc;
764}
765#endif
766
767
768/**
769 * Helper function to create/start a process on the guest.
770 *
771 * @return IPRT status code.
772 * @param pszExec Full qualified path of process to start (without arguments).
773 * @param papszArgs Pointer to array of command line arguments.
774 * @param hEnv Handle to environment block to use.
775 * @param fFlags Process execution flags.
776 * @param phStdIn Handle for the process' stdin pipe.
777 * @param phStdOut Handle for the process' stdout pipe.
778 * @param phStdErr Handle for the process' stderr pipe.
779 * @param pszAsUser User name (account) to start the process under.
780 * @param pszPassword Password of the specified user.
781 * @param phProcess Pointer which will receive the process handle after
782 * successful process start.
783 */
784static int VBoxServiceControlExecCreateProcess(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
785 PCRTHANDLE phStdIn, PCRTHANDLE phStdOut, PCRTHANDLE phStdErr, const char *pszAsUser,
786 const char *pszPassword, PRTPROCESS phProcess)
787{
788 AssertPtrReturn(pszExec, VERR_INVALID_PARAMETER);
789 AssertPtrReturn(papszArgs, VERR_INVALID_PARAMETER);
790 AssertPtrReturn(phProcess, VERR_INVALID_PARAMETER);
791
792 int rc = VINF_SUCCESS;
793 char szExecExp[RTPATH_MAX];
794#ifdef RT_OS_WINDOWS
795 /*
796 * If sysprep should be executed do this in the context of VBoxService, which
797 * (usually, if started by SCM) has administrator rights. Because of that a UI
798 * won't be shown (doesn't have a desktop).
799 */
800 if (RTStrICmp(pszExec, "sysprep") == 0)
801 {
802 /* Use a predefined sysprep path as default. */
803 char szSysprepCmd[RTPATH_MAX] = "C:\\sysprep\\sysprep.exe";
804
805 /*
806 * On Windows Vista (and up) sysprep is located in "system32\\sysprep\\sysprep.exe",
807 * so detect the OS and use a different path.
808 */
809 OSVERSIONINFOEX OSInfoEx;
810 RT_ZERO(OSInfoEx);
811 OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
812 if ( GetVersionEx((LPOSVERSIONINFO) &OSInfoEx)
813 && OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
814 && OSInfoEx.dwMajorVersion >= 6 /* Vista or later */)
815 {
816 rc = RTEnvGetEx(RTENV_DEFAULT, "windir", szSysprepCmd, sizeof(szSysprepCmd), NULL);
817 if (RT_SUCCESS(rc))
818 rc = RTPathAppend(szSysprepCmd, sizeof(szSysprepCmd), "system32\\sysprep\\sysprep.exe");
819 }
820
821 if (RT_SUCCESS(rc))
822 {
823 char **papszArgsExp;
824 rc = VBoxServiceControlExecPrepareArgv(szSysprepCmd /* argv0 */, &papszArgs[1], &papszArgsExp);
825 if (RT_SUCCESS(rc))
826 {
827 rc = RTProcCreateEx(szSysprepCmd, papszArgsExp, hEnv, 0 /* fFlags */,
828 phStdIn, phStdOut, phStdErr, NULL /* pszAsUser */,
829 NULL /* pszPassword */, phProcess);
830 }
831 RTGetOptArgvFree(papszArgsExp);
832 }
833 return rc;
834 }
835#endif /* RT_OS_WINDOWS */
836
837 /*
838 * Do the environment variables expansion on executable and arguments.
839 */
840 rc = VBoxServiceControlExecResolveExecutable(pszExec, szExecExp, sizeof(szExecExp));
841 if (RT_SUCCESS(rc))
842 {
843 char **papszArgsExp;
844 rc = VBoxServiceControlExecPrepareArgv(szExecExp /* argv0 */, &papszArgs[1], &papszArgsExp);
845 if (RT_SUCCESS(rc))
846 {
847 uint32_t uProcFlags = 0;
848 if (fFlags)
849 {
850 /* Process Main flag "ExecuteProcessFlag_Hidden". */
851 if (fFlags & RT_BIT(2))
852 uProcFlags = RTPROC_FLAGS_HIDDEN;
853 }
854
855 /* If no user name specified run with current credentials (e.g.
856 * full service/system rights). This is prohibited via official Main API!
857 *
858 * Otherwise use the RTPROC_FLAGS_SERVICE to use some special authentication
859 * code (at least on Windows) for running processes as different users
860 * started from our system service. */
861 if (*pszAsUser)
862 uProcFlags |= RTPROC_FLAGS_SERVICE;
863#ifdef DEBUG
864 char *pszCmdLine;
865 rc = RTGetOptArgvToString(&pszCmdLine, papszArgsExp, 0 /* Default */);
866 AssertRC(rc);
867 VBoxServiceVerbose(3, "ControlExec: Executing: %s %s\n",
868 szExecExp, pszCmdLine);
869 RTStrFree(pszCmdLine);
870#endif
871 /* Do normal execution. */
872 rc = RTProcCreateEx(szExecExp, papszArgsExp, hEnv, uProcFlags,
873 phStdIn, phStdOut, phStdErr,
874 *pszAsUser ? pszAsUser : NULL,
875 *pszPassword ? pszPassword : NULL,
876 phProcess);
877 RTGetOptArgvFree(papszArgsExp);
878 }
879 }
880 return rc;
881}
882
883/**
884 * The actual worker routine (lopp) for a started guest process.
885 *
886 * @return IPRT status code.
887 * @param PVBOXSERVICECTRLTHREAD Thread data associated with a started process.
888 */
889static DECLCALLBACK(int) VBoxServiceControlExecProcessWorker(PVBOXSERVICECTRLTHREAD pThread)
890{
891 AssertPtr(pThread);
892 PVBOXSERVICECTRLTHREADDATAEXEC pData = (PVBOXSERVICECTRLTHREADDATAEXEC)pThread->pvData;
893 AssertPtr(pData);
894
895 VBoxServiceVerbose(3, "ControlExec: Thread of process \"%s\" started\n", pData->pszCmd);
896
897 int rc = VbglR3GuestCtrlConnect(&pThread->uClientID);
898 if (RT_FAILURE(rc))
899 {
900 VBoxServiceError("ControlExec: Thread failed to connect to the guest control service, aborted! Error: %Rrc\n", rc);
901 RTThreadUserSignal(RTThreadSelf());
902 return rc;
903 }
904
905 bool fSignalled = false; /* Indicator whether we signalled the thread user event already. */
906
907 /*
908 * Create the environment.
909 */
910 RTENV hEnv;
911 rc = RTEnvClone(&hEnv, RTENV_DEFAULT);
912 if (RT_SUCCESS(rc))
913 {
914 size_t i;
915 for (i = 0; i < pData->uNumEnvVars && pData->papszEnv; i++)
916 {
917 rc = RTEnvPutEx(hEnv, pData->papszEnv[i]);
918 if (RT_FAILURE(rc))
919 break;
920 }
921 if (RT_SUCCESS(rc))
922 {
923 /*
924 * Setup the redirection of the standard stuff.
925 */
926 /** @todo consider supporting: gcc stuff.c >file 2>&1. */
927 RTHANDLE hStdIn;
928 PRTHANDLE phStdIn;
929 rc = VBoxServiceControlExecSetupPipe(0 /*STDIN_FILENO*/, &hStdIn, &phStdIn, &pData->pipeStdInW);
930 if (RT_SUCCESS(rc))
931 {
932 RTHANDLE hStdOut;
933 PRTHANDLE phStdOut;
934 RTPIPE hStdOutR;
935 rc = VBoxServiceControlExecSetupPipe(1 /*STDOUT_FILENO*/, &hStdOut, &phStdOut, &hStdOutR);
936 if (RT_SUCCESS(rc))
937 {
938 RTHANDLE hStdErr;
939 PRTHANDLE phStdErr;
940 RTPIPE hStdErrR;
941 rc = VBoxServiceControlExecSetupPipe(2 /*STDERR_FILENO*/, &hStdErr, &phStdErr, &hStdErrR);
942 if (RT_SUCCESS(rc))
943 {
944 /*
945 * Create a poll set for the pipes and let the
946 * transport layer add stuff to it as well.
947 */
948 RTPOLLSET hPollSet;
949 rc = RTPollSetCreate(&hPollSet);
950 if (RT_SUCCESS(rc))
951 {
952 rc = RTPollSetAddPipe(hPollSet, pData->pipeStdInW, RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDIN_ERROR);
953 if (RT_SUCCESS(rc))
954 rc = RTPollSetAddPipe(hPollSet, hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDOUT);
955 if (RT_SUCCESS(rc))
956 rc = RTPollSetAddPipe(hPollSet, hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR, VBOXSERVICECTRLPIPEID_STDERR);
957 if (RT_SUCCESS(rc))
958 rc = RTPollSetAddPipe(hPollSet, pData->pipeStdInW, RTPOLL_EVT_WRITE, VBOXSERVICECTRLPIPEID_STDIN_WRITABLE);
959 if (RT_SUCCESS(rc))
960 rc = RTPollSetAddPipe(hPollSet, pData->stdIn.hNotificationPipeR, RTPOLL_EVT_READ, VBOXSERVICECTRLPIPEID_STDIN_INPUT_NOTIFY);
961 if (RT_SUCCESS(rc))
962 {
963 RTPROCESS hProcess;
964 rc = VBoxServiceControlExecCreateProcess(pData->pszCmd, pData->papszArgs, hEnv, pData->uFlags,
965 phStdIn, phStdOut, phStdErr,
966 pData->pszUser, pData->pszPassword,
967 &hProcess);
968 if (RT_FAILURE(rc))
969 VBoxServiceError("ControlExec: Error starting process, rc=%Rrc\n", rc);
970 /*
971 * Tell the control thread that it can continue
972 * spawning services. This needs to be done after the new
973 * process has been started because otherwise signal handling
974 * on (Open) Solaris does not work correctly (see #5068).
975 */
976 int rc2 = RTThreadUserSignal(RTThreadSelf());
977 if (RT_FAILURE(rc2))
978 rc = rc2;
979 fSignalled = true;
980
981 if (RT_SUCCESS(rc))
982 {
983 /*
984 * Close the child ends of any pipes and redirected files.
985 */
986 rc2 = RTHandleClose(phStdIn); AssertRC(rc2);
987 phStdIn = NULL;
988 rc2 = RTHandleClose(phStdOut); AssertRC(rc2);
989 phStdOut = NULL;
990 rc2 = RTHandleClose(phStdErr); AssertRC(rc2);
991 phStdErr = NULL;
992
993 /* Enter the process loop. */
994 rc = VBoxServiceControlExecProcLoop(pThread,
995 hProcess, pData->uTimeLimitMS, hPollSet,
996 &pData->pipeStdInW, &hStdOutR, &hStdErrR);
997
998 /*
999 * The handles that are no longer in the set have
1000 * been closed by the above call in order to prevent
1001 * the guest from getting stuck accessing them.
1002 * So, NIL the handles to avoid closing them again.
1003 */
1004 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, 0 /* stdin */, NULL)))
1005 pData->pipeStdInW = NIL_RTPIPE;
1006 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, 1 /* stdout */, NULL)))
1007 hStdOutR = NIL_RTPIPE;
1008 if (RT_FAILURE(RTPollSetQueryHandle(hPollSet, 2 /* stderr */, NULL)))
1009 hStdErrR = NIL_RTPIPE;
1010 }
1011 else /* Something went wrong; report error! */
1012 {
1013 VBoxServiceError("ControlExec: Could not start process '%s' (CID: %u)! Error: %Rrc\n",
1014 pData->pszCmd, pThread->uContextID, rc);
1015
1016 rc2 = VbglR3GuestCtrlExecReportStatus(pThread->uClientID, pThread->uContextID, pData->uPID,
1017 PROC_STS_ERROR, rc,
1018 NULL /* pvData */, 0 /* cbData */);
1019 if (RT_FAILURE(rc2))
1020 VBoxServiceError("ControlExec: Could not report process start error! Error: %Rrc (process error %Rrc)\n",
1021 rc2, rc);
1022 }
1023 }
1024 RTPollSetDestroy(hPollSet);
1025 }
1026 RTPipeClose(hStdErrR);
1027 RTHandleClose(phStdErr);
1028 }
1029 RTPipeClose(hStdOutR);
1030 RTHandleClose(phStdOut);
1031 }
1032 RTPipeClose(pData->pipeStdInW);
1033 RTHandleClose(phStdIn);
1034 }
1035 }
1036 RTEnvDestroy(hEnv);
1037 }
1038
1039 VbglR3GuestCtrlDisconnect(pThread->uClientID);
1040 VBoxServiceVerbose(3, "ControlExec: Thread of process \"%s\" (PID: %u) ended with rc=%Rrc\n",
1041 pData->pszCmd, pData->uPID, rc);
1042
1043 /*
1044 * If something went wrong signal the user event so that others don't wait
1045 * forever on this thread.
1046 */
1047 if (RT_FAILURE(rc) && !fSignalled)
1048 RTThreadUserSignal(RTThreadSelf());
1049 return rc;
1050}
1051
1052
1053/**
1054 * Thread main routine for a started process.
1055 *
1056 * @return IPRT status code.
1057 * @param RTTHREAD Pointer to the thread's data.
1058 * @param void* User-supplied argument pointer.
1059 *
1060 */
1061static DECLCALLBACK(int) VBoxServiceControlExecThread(RTTHREAD ThreadSelf, void *pvUser)
1062{
1063 PVBOXSERVICECTRLTHREAD pThread = (VBOXSERVICECTRLTHREAD*)pvUser;
1064 AssertPtr(pThread);
1065 return VBoxServiceControlExecProcessWorker(pThread);
1066}
1067
1068
1069/**
1070 * Executes (starts) a process on the guest. This causes a new thread to be created
1071 * so that this function will not block the overall program execution.
1072 *
1073 * @return IPRT status code.
1074 * @param uContextID Context ID to associate the process to start with.
1075 * @param pszCmd Full qualified path of process to start (without arguments).
1076 * @param uFlags Process execution flags.
1077 * @param pszArgs String of arguments to pass to the process to start.
1078 * @param uNumArgs Number of arguments specified in pszArgs.
1079 * @param pszEnv String of environment variables ("FOO=BAR") to pass to the process
1080 * to start.
1081 * @param cbEnv Size (in bytes) of environment variables.
1082 * @param uNumEnvVars Number of environment variables specified in pszEnv.
1083 * @param pszUser User name (account) to start the process under.
1084 * @param pszPassword Password of specified user name (account).
1085 * @param uTimeLimitMS Time limit (in ms) of the process' life time.
1086 */
1087int VBoxServiceControlExecProcess(uint32_t uContextID, const char *pszCmd, uint32_t uFlags,
1088 const char *pszArgs, uint32_t uNumArgs,
1089 const char *pszEnv, uint32_t cbEnv, uint32_t uNumEnvVars,
1090 const char *pszUser, const char *pszPassword, uint32_t uTimeLimitMS)
1091{
1092 int rc;
1093
1094 PVBOXSERVICECTRLTHREAD pThread = (PVBOXSERVICECTRLTHREAD)RTMemAlloc(sizeof(VBOXSERVICECTRLTHREAD));
1095 if (pThread)
1096 {
1097 rc = VBoxServiceControlExecThreadAlloc(pThread,
1098 uContextID,
1099 pszCmd, uFlags,
1100 pszArgs, uNumArgs,
1101 pszEnv, cbEnv, uNumEnvVars,
1102 pszUser, pszPassword,
1103 uTimeLimitMS);
1104 if (RT_SUCCESS(rc))
1105 {
1106 rc = RTThreadCreate(&pThread->Thread, VBoxServiceControlExecThread,
1107 (void *)(PVBOXSERVICECTRLTHREAD*)pThread, 0,
1108 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "Exec");
1109 if (RT_FAILURE(rc))
1110 {
1111 VBoxServiceError("ControlExec: RTThreadCreate failed, rc=%Rrc\n, pThread=%p\n",
1112 rc, pThread);
1113 }
1114 else
1115 {
1116 VBoxServiceVerbose(4, "ControlExec: Waiting for thread to initialize ...\n");
1117
1118 /* Wait for the thread to initialize. */
1119 RTThreadUserWait(pThread->Thread, 60 * 1000);
1120 if (pThread->fShutdown)
1121 {
1122 VBoxServiceError("ControlExec: Thread for process \"%s\" failed to start!\n", pszCmd);
1123 rc = VERR_GENERAL_FAILURE;
1124 }
1125 else
1126 {
1127 pThread->fStarted = true;
1128 /*rc =*/ RTListAppend(&g_GuestControlExecThreads, &pThread->Node);
1129 }
1130 }
1131
1132 if (RT_FAILURE(rc))
1133 VBoxServiceControlExecThreadDestroy((PVBOXSERVICECTRLTHREADDATAEXEC)pThread->pvData);
1134 }
1135 if (RT_FAILURE(rc))
1136 RTMemFree(pThread);
1137 }
1138 else
1139 rc = VERR_NO_MEMORY;
1140 return rc;
1141}
1142
1143
1144/**
1145 * Handles starting processes on the guest.
1146 *
1147 * @returns IPRT status code.
1148 * @param u32ClientId The HGCM client session ID.
1149 * @param uNumParms The number of parameters the host is offering.
1150 */
1151int VBoxServiceControlExecHandleCmdStartProcess(uint32_t u32ClientId, uint32_t uNumParms)
1152{
1153 uint32_t uContextID;
1154 char szCmd[_1K];
1155 uint32_t uFlags;
1156 char szArgs[_1K];
1157 uint32_t uNumArgs;
1158 char szEnv[_64K];
1159 uint32_t cbEnv = sizeof(szEnv);
1160 uint32_t uNumEnvVars;
1161 char szUser[128];
1162 char szPassword[128];
1163 uint32_t uTimeLimitMS;
1164
1165#if 0 /* for valgrind */
1166 RT_ZERO(szCmd);
1167 RT_ZERO(szArgs);
1168 RT_ZERO(szEnv);
1169 RT_ZERO(szUser);
1170 RT_ZERO(szPassword);
1171#endif
1172
1173 if (uNumParms != 11)
1174 return VERR_INVALID_PARAMETER;
1175
1176 int rc = VbglR3GuestCtrlExecGetHostCmd(u32ClientId,
1177 uNumParms,
1178 &uContextID,
1179 /* Command */
1180 szCmd, sizeof(szCmd),
1181 /* Flags */
1182 &uFlags,
1183 /* Arguments */
1184 szArgs, sizeof(szArgs), &uNumArgs,
1185 /* Environment */
1186 szEnv, &cbEnv, &uNumEnvVars,
1187 /* Credentials */
1188 szUser, sizeof(szUser),
1189 szPassword, sizeof(szPassword),
1190 /* Timelimit */
1191 &uTimeLimitMS);
1192#ifdef DEBUG
1193 VBoxServiceVerbose(3, "ControlExec: Start process szCmd=%s, uFlags=%u, szArgs=%s, szEnv=%s, szUser=%s, szPW=%s, uTimeout=%u\n",
1194 szCmd, uFlags, uNumArgs ? szArgs : "<None>", uNumEnvVars ? szEnv : "<None>", szUser, szPassword, uTimeLimitMS);
1195#endif
1196 if (RT_SUCCESS(rc))
1197 {
1198 rc = VBoxServiceControlExecProcess(uContextID, szCmd, uFlags, szArgs, uNumArgs,
1199 szEnv, cbEnv, uNumEnvVars,
1200 szUser, szPassword, uTimeLimitMS);
1201 }
1202 else
1203 VBoxServiceError("ControlExec: Failed to retrieve exec start command! Error: %Rrc\n", rc);
1204 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecHandleCmdStartProcess returned with %Rrc\n", rc);
1205 return rc;
1206}
1207
1208
1209/**
1210 * Handles input for a started process by copying the received data into its
1211 * stdin pipe.
1212 *
1213 * @returns IPRT status code.
1214 * @param u32ClientId The HGCM client session ID.
1215 * @param uNumParms The number of parameters the host is offering.
1216 * @param cMaxBufSize The maximum buffer size for retrieving the input data.
1217 */
1218int VBoxServiceControlExecHandleCmdSetInput(uint32_t u32ClientId, uint32_t uNumParms, size_t cbMaxBufSize)
1219{
1220 uint32_t uContextID;
1221 uint32_t uPID;
1222 uint32_t uFlags;
1223 uint32_t cbSize;
1224
1225 AssertReturn(RT_IS_POWER_OF_TWO(cbMaxBufSize), VERR_INVALID_PARAMETER);
1226 uint8_t *pabBuffer = (uint8_t*)RTMemAlloc(cbMaxBufSize);
1227 AssertPtrReturn(pabBuffer, VERR_NO_MEMORY);
1228
1229 uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
1230 uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
1231
1232 /*
1233 * Ask the host for the input data.
1234 */
1235 int rc = VbglR3GuestCtrlExecGetHostCmdInput(u32ClientId, uNumParms,
1236 &uContextID, &uPID, &uFlags,
1237 pabBuffer, cbMaxBufSize, &cbSize);
1238 if (RT_FAILURE(rc))
1239 {
1240 VBoxServiceError("ControlExec: Failed to retrieve exec input command! Error: %Rrc\n", rc);
1241 }
1242 else if (cbSize > cbMaxBufSize)
1243 {
1244 VBoxServiceError("ControlExec: Maximum input buffer size is too small! cbSize=%u, cbMaxBufSize=%u\n",
1245 cbSize, cbMaxBufSize);
1246 rc = VERR_INVALID_PARAMETER;
1247 }
1248 else
1249 {
1250 /*
1251 * Is this the last input block we need to deliver? Then let the pipe know ...
1252 */
1253 bool fPendingClose = false;
1254 if (uFlags & INPUT_FLAG_EOF)
1255 {
1256 fPendingClose = true;
1257 VBoxServiceVerbose(4, "ControlExec: Got last input block (PID %u) of size %u ...\n", uPID, cbSize);
1258 }
1259
1260 rc = VBoxServiceControlExecThreadSetInput(uPID, fPendingClose, pabBuffer,
1261 cbSize, &cbWritten);
1262 VBoxServiceVerbose(4, "ControlExec: Written input (PID %u): rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
1263 uPID, rc, uFlags, fPendingClose, cbSize, cbWritten);
1264 if (RT_SUCCESS(rc))
1265 {
1266 if (cbWritten || !cbSize) /* Did we write something or was there anything to write at all? */
1267 {
1268 uStatus = INPUT_STS_WRITTEN;
1269 uFlags = 0;
1270 }
1271 }
1272 else
1273 {
1274 if (rc == VERR_BAD_PIPE)
1275 uStatus = INPUT_STS_TERMINATED;
1276 else if (rc == VERR_BUFFER_OVERFLOW)
1277 uStatus = INPUT_STS_OVERFLOW;
1278 }
1279 }
1280 RTMemFree(pabBuffer);
1281
1282 /*
1283 * If there was an error and we did not set the host status
1284 * yet, then do it now.
1285 */
1286 if ( RT_FAILURE(rc)
1287 && uStatus == INPUT_STS_UNDEFINED)
1288 {
1289 uStatus = INPUT_STS_ERROR;
1290 uFlags = rc;
1291 }
1292 Assert(uStatus > INPUT_STS_UNDEFINED);
1293
1294 VBoxServiceVerbose(3, "ControlExec: Input processed (PID %u), Status=%u, Flags=0x%x, cbWritten=%u\n",
1295 uPID, uStatus, uFlags, cbWritten);
1296
1297 /* Note: Since the context ID is unique the request *has* to be completed here,
1298 * regardless whether we got data or not! Otherwise the progress object
1299 * on the host never will get completed! */
1300 rc = VbglR3GuestCtrlExecReportStatusIn(u32ClientId, uContextID, uPID,
1301 uStatus, uFlags, (uint32_t)cbWritten);
1302
1303 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecHandleCmdSetInput returned with %Rrc\n", rc);
1304 return rc;
1305}
1306
1307
1308/**
1309 * Handles the guest control output command.
1310 *
1311 * @return IPRT status code.
1312 * @param u32ClientId idClient The HGCM client session ID.
1313 * @param uNumParms cParms The number of parameters the host is
1314 * offering.
1315 */
1316int VBoxServiceControlExecHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms)
1317{
1318 uint32_t uContextID;
1319 uint32_t uPID;
1320 uint32_t uHandleID;
1321 uint32_t uFlags;
1322
1323 int rc = VbglR3GuestCtrlExecGetHostCmdOutput(u32ClientId, uNumParms,
1324 &uContextID, &uPID, &uHandleID, &uFlags);
1325 if (RT_SUCCESS(rc))
1326 {
1327 uint32_t cbRead;
1328 uint8_t *pBuf = (uint8_t*)RTMemAlloc(_64K);
1329 if (pBuf)
1330 {
1331 rc = VBoxServiceControlExecThreadGetOutput(uPID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
1332 pBuf, _64K /* cbSize */, &cbRead);
1333 if (RT_SUCCESS(rc))
1334 {
1335 /* Note: Since the context ID is unique the request *has* to be completed here,
1336 * regardless whether we got data or not! Otherwise the progress object
1337 * on the host never will get completed! */
1338 /* cbRead now contains actual size. */
1339 rc = VbglR3GuestCtrlExecSendOut(u32ClientId, uContextID, uPID, uHandleID, 0 /* Flags */,
1340 pBuf, cbRead);
1341 }
1342 RTMemFree(pBuf);
1343 }
1344 else
1345 rc = VERR_NO_MEMORY;
1346 }
1347 else
1348 VBoxServiceError("ControlExec: Failed to retrieve exec output command! Error: %Rrc\n", rc);
1349 VBoxServiceVerbose(3, "ControlExec: VBoxServiceControlExecHandleCmdGetOutput returned with %Rrc\n", rc);
1350 return rc;
1351}
1352
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