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