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