1 | /* $Id: VBoxServiceControlSession.cpp 69498 2017-10-28 15:07:25Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceControlSession - Guest session handling. Also handles the spawned session processes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2016 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/dir.h>
|
---|
25 | #include <iprt/env.h>
|
---|
26 | #include <iprt/file.h>
|
---|
27 | #include <iprt/getopt.h>
|
---|
28 | #include <iprt/handle.h>
|
---|
29 | #include <iprt/mem.h>
|
---|
30 | #include <iprt/message.h>
|
---|
31 | #include <iprt/path.h>
|
---|
32 | #include <iprt/pipe.h>
|
---|
33 | #include <iprt/poll.h>
|
---|
34 | #include <iprt/process.h>
|
---|
35 |
|
---|
36 | #include "VBoxServiceInternal.h"
|
---|
37 | #include "VBoxServiceUtils.h"
|
---|
38 | #include "VBoxServiceControl.h"
|
---|
39 |
|
---|
40 | using namespace guestControl;
|
---|
41 |
|
---|
42 |
|
---|
43 | /*********************************************************************************************************************************
|
---|
44 | * Structures and Typedefs *
|
---|
45 | *********************************************************************************************************************************/
|
---|
46 | /** Generic option indices for session spawn arguments. */
|
---|
47 | enum
|
---|
48 | {
|
---|
49 | VBOXSERVICESESSIONOPT_FIRST = 1000, /* For initialization. */
|
---|
50 | VBOXSERVICESESSIONOPT_DOMAIN,
|
---|
51 | #ifdef DEBUG
|
---|
52 | VBOXSERVICESESSIONOPT_DUMP_STDOUT,
|
---|
53 | VBOXSERVICESESSIONOPT_DUMP_STDERR,
|
---|
54 | #endif
|
---|
55 | VBOXSERVICESESSIONOPT_LOG_FILE,
|
---|
56 | VBOXSERVICESESSIONOPT_USERNAME,
|
---|
57 | VBOXSERVICESESSIONOPT_SESSION_ID,
|
---|
58 | VBOXSERVICESESSIONOPT_SESSION_PROTO,
|
---|
59 | VBOXSERVICESESSIONOPT_THREAD_ID
|
---|
60 | };
|
---|
61 |
|
---|
62 |
|
---|
63 |
|
---|
64 | static int vgsvcGstCtrlSessionFileDestroy(PVBOXSERVICECTRLFILE pFile)
|
---|
65 | {
|
---|
66 | AssertPtrReturn(pFile, VERR_INVALID_POINTER);
|
---|
67 |
|
---|
68 | int rc = RTFileClose(pFile->hFile);
|
---|
69 | if (RT_SUCCESS(rc))
|
---|
70 | {
|
---|
71 | /* Remove file entry in any case. */
|
---|
72 | RTListNodeRemove(&pFile->Node);
|
---|
73 | /* Destroy this object. */
|
---|
74 | RTMemFree(pFile);
|
---|
75 | }
|
---|
76 |
|
---|
77 | return rc;
|
---|
78 | }
|
---|
79 |
|
---|
80 |
|
---|
81 | /** @todo No locking done yet! */
|
---|
82 | static PVBOXSERVICECTRLFILE vgsvcGstCtrlSessionFileGetLocked(const PVBOXSERVICECTRLSESSION pSession, uint32_t uHandle)
|
---|
83 | {
|
---|
84 | AssertPtrReturn(pSession, NULL);
|
---|
85 |
|
---|
86 | /** @todo Use a map later! */
|
---|
87 | PVBOXSERVICECTRLFILE pFileCur;
|
---|
88 | RTListForEach(&pSession->lstFiles, pFileCur, VBOXSERVICECTRLFILE, Node)
|
---|
89 | {
|
---|
90 | if (pFileCur->uHandle == uHandle)
|
---|
91 | return pFileCur;
|
---|
92 | }
|
---|
93 |
|
---|
94 | return NULL;
|
---|
95 | }
|
---|
96 |
|
---|
97 |
|
---|
98 | static int vgsvcGstCtrlSessionHandleDirRemove(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
99 | {
|
---|
100 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
101 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
102 |
|
---|
103 | char szDir[RTPATH_MAX];
|
---|
104 | uint32_t fFlags = 0;
|
---|
105 |
|
---|
106 | int rc = VbglR3GuestCtrlDirGetRemove(pHostCtx,
|
---|
107 | /* Directory to remove. */
|
---|
108 | szDir, sizeof(szDir),
|
---|
109 | /* Flags of type DIRREMOVE_FLAG_. */
|
---|
110 | &fFlags);
|
---|
111 | if (RT_SUCCESS(rc))
|
---|
112 | {
|
---|
113 | AssertReturn(!(fFlags & ~DIRREMOVE_FLAG_VALID_MASK), VERR_INVALID_PARAMETER);
|
---|
114 | if (!(fFlags & ~DIRREMOVE_FLAG_VALID_MASK))
|
---|
115 | {
|
---|
116 | if (fFlags & DIRREMOVE_FLAG_RECURSIVE)
|
---|
117 | {
|
---|
118 | uint32_t fFlagsRemRec = RTDIRRMREC_F_CONTENT_AND_DIR; /* Set default. */
|
---|
119 | if (fFlags & DIRREMOVE_FLAG_CONTENT_ONLY)
|
---|
120 | fFlagsRemRec |= RTDIRRMREC_F_CONTENT_ONLY;
|
---|
121 |
|
---|
122 | rc = RTDirRemoveRecursive(szDir, fFlagsRemRec);
|
---|
123 | }
|
---|
124 | else /* Only delete directory if not empty. */
|
---|
125 | rc = RTDirRemove(szDir);
|
---|
126 | }
|
---|
127 | else
|
---|
128 | rc = VERR_NOT_SUPPORTED;
|
---|
129 |
|
---|
130 | VGSvcVerbose(4, "[Dir %s]: Removing with fFlags=0x%x, rc=%Rrc\n", szDir, fFlags, rc);
|
---|
131 |
|
---|
132 | /* Report back in any case. */
|
---|
133 | int rc2 = VbglR3GuestCtrlMsgReply(pHostCtx, rc);
|
---|
134 | if (RT_FAILURE(rc2))
|
---|
135 | VGSvcError("[Dir %s]: Failed to report removing status, rc=%Rrc\n", szDir, rc2);
|
---|
136 | if (RT_SUCCESS(rc))
|
---|
137 | rc = rc2;
|
---|
138 | }
|
---|
139 |
|
---|
140 | #ifdef DEBUG
|
---|
141 | VGSvcVerbose(4, "Removing directory '%s' returned rc=%Rrc\n", szDir, rc);
|
---|
142 | #endif
|
---|
143 | return rc;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | static int vgsvcGstCtrlSessionHandleFileOpen(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
148 | {
|
---|
149 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
150 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
151 |
|
---|
152 | char szFile[RTPATH_MAX];
|
---|
153 | char szAccess[64];
|
---|
154 | char szDisposition[64];
|
---|
155 | char szSharing[64];
|
---|
156 | uint32_t uCreationMode = 0;
|
---|
157 | uint64_t offOpen = 0;
|
---|
158 | uint32_t uHandle = 0;
|
---|
159 |
|
---|
160 | int rc = VbglR3GuestCtrlFileGetOpen(pHostCtx,
|
---|
161 | /* File to open. */
|
---|
162 | szFile, sizeof(szFile),
|
---|
163 | /* Open mode. */
|
---|
164 | szAccess, sizeof(szAccess),
|
---|
165 | /* Disposition. */
|
---|
166 | szDisposition, sizeof(szDisposition),
|
---|
167 | /* Sharing. */
|
---|
168 | szSharing, sizeof(szSharing),
|
---|
169 | /* Creation mode. */
|
---|
170 | &uCreationMode,
|
---|
171 | /* Offset. */
|
---|
172 | &offOpen);
|
---|
173 | VGSvcVerbose(4, "[File %s]: szAccess=%s, szDisposition=%s, szSharing=%s, offOpen=%RU64, rc=%Rrc\n",
|
---|
174 | szFile, szAccess, szDisposition, szSharing, offOpen, rc);
|
---|
175 | if (RT_SUCCESS(rc))
|
---|
176 | {
|
---|
177 | PVBOXSERVICECTRLFILE pFile = (PVBOXSERVICECTRLFILE)RTMemAllocZ(sizeof(VBOXSERVICECTRLFILE));
|
---|
178 | if (pFile)
|
---|
179 | {
|
---|
180 | if (!strlen(szFile))
|
---|
181 | rc = VERR_INVALID_PARAMETER;
|
---|
182 |
|
---|
183 | if (RT_SUCCESS(rc))
|
---|
184 | {
|
---|
185 | /** @todo r=bird: Plase, use RTStrCopy for stuff like this! */
|
---|
186 | RTStrPrintf(pFile->szName, sizeof(pFile->szName), "%s", szFile);
|
---|
187 |
|
---|
188 | uint64_t fFlags;
|
---|
189 | rc = RTFileModeToFlagsEx(szAccess, szDisposition, NULL /* pszSharing, not used yet */, &fFlags);
|
---|
190 | VGSvcVerbose(4, "[File %s]: Opening with fFlags=0x%x, rc=%Rrc\n", pFile->szName, fFlags, rc);
|
---|
191 |
|
---|
192 | if (RT_SUCCESS(rc))
|
---|
193 | rc = RTFileOpen(&pFile->hFile, pFile->szName, fFlags);
|
---|
194 | if ( RT_SUCCESS(rc)
|
---|
195 | && offOpen)
|
---|
196 | {
|
---|
197 | /* Seeking is optional. However, the whole operation
|
---|
198 | * will fail if we don't succeed seeking to the wanted position. */
|
---|
199 | rc = RTFileSeek(pFile->hFile, (int64_t)offOpen, RTFILE_SEEK_BEGIN, NULL /* Current offset */);
|
---|
200 | if (RT_FAILURE(rc))
|
---|
201 | VGSvcError("[File %s]: Seeking to offset %RU64 failed; rc=%Rrc\n", pFile->szName, offOpen, rc);
|
---|
202 | }
|
---|
203 | else if (RT_FAILURE(rc))
|
---|
204 | VGSvcError("[File %s]: Opening failed with rc=%Rrc\n", pFile->szName, rc);
|
---|
205 | }
|
---|
206 |
|
---|
207 | if (RT_SUCCESS(rc))
|
---|
208 | {
|
---|
209 | uHandle = VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pHostCtx->uContextID);
|
---|
210 | pFile->uHandle = uHandle;
|
---|
211 |
|
---|
212 | RTListAppend(&pSession->lstFiles, &pFile->Node);
|
---|
213 |
|
---|
214 | VGSvcVerbose(3, "[File %s]: Opened (ID=%RU32)\n", pFile->szName, pFile->uHandle);
|
---|
215 | }
|
---|
216 |
|
---|
217 | if (RT_FAILURE(rc))
|
---|
218 | {
|
---|
219 | if (pFile->hFile)
|
---|
220 | RTFileClose(pFile->hFile);
|
---|
221 | RTMemFree(pFile);
|
---|
222 | }
|
---|
223 | }
|
---|
224 | else
|
---|
225 | rc = VERR_NO_MEMORY;
|
---|
226 |
|
---|
227 | /* Report back in any case. */
|
---|
228 | int rc2 = VbglR3GuestCtrlFileCbOpen(pHostCtx, rc, uHandle);
|
---|
229 | if (RT_FAILURE(rc2))
|
---|
230 | VGSvcError("[File %s]: Failed to report file open status, rc=%Rrc\n", szFile, rc2);
|
---|
231 | if (RT_SUCCESS(rc))
|
---|
232 | rc = rc2;
|
---|
233 | }
|
---|
234 |
|
---|
235 | #ifdef DEBUG
|
---|
236 | VGSvcVerbose(4, "Opening file '%s' (open mode='%s', disposition='%s', creation mode=0x%x returned rc=%Rrc\n",
|
---|
237 | szFile, szAccess, szDisposition, uCreationMode, rc);
|
---|
238 | #endif
|
---|
239 | return rc;
|
---|
240 | }
|
---|
241 |
|
---|
242 |
|
---|
243 | static int vgsvcGstCtrlSessionHandleFileClose(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
244 | {
|
---|
245 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
246 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
247 |
|
---|
248 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
249 |
|
---|
250 | uint32_t uHandle = 0;
|
---|
251 | int rc = VbglR3GuestCtrlFileGetClose(pHostCtx, &uHandle /* File handle to close */);
|
---|
252 | if (RT_SUCCESS(rc))
|
---|
253 | {
|
---|
254 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
255 | if (pFile)
|
---|
256 | rc = vgsvcGstCtrlSessionFileDestroy(pFile);
|
---|
257 | else
|
---|
258 | rc = VERR_NOT_FOUND;
|
---|
259 |
|
---|
260 | /* Report back in any case. */
|
---|
261 | int rc2 = VbglR3GuestCtrlFileCbClose(pHostCtx, rc);
|
---|
262 | if (RT_FAILURE(rc2))
|
---|
263 | VGSvcError("Failed to report file close status, rc=%Rrc\n", rc2);
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | rc = rc2;
|
---|
266 | }
|
---|
267 |
|
---|
268 | #ifdef DEBUG
|
---|
269 | VGSvcVerbose(4, "Closing file '%s' (handle=%RU32) returned rc=%Rrc\n", pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
270 | #endif
|
---|
271 | return rc;
|
---|
272 | }
|
---|
273 |
|
---|
274 |
|
---|
275 | static int vgsvcGstCtrlSessionHandleFileRead(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
276 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
277 | {
|
---|
278 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
279 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
280 |
|
---|
281 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
282 |
|
---|
283 | uint32_t uHandle = 0;
|
---|
284 | uint32_t cbToRead;
|
---|
285 | int rc = VbglR3GuestCtrlFileGetRead(pHostCtx, &uHandle, &cbToRead);
|
---|
286 | if (RT_SUCCESS(rc))
|
---|
287 | {
|
---|
288 | void *pvDataRead = pvScratchBuf;
|
---|
289 | size_t cbRead = 0;
|
---|
290 |
|
---|
291 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
292 | if (pFile)
|
---|
293 | {
|
---|
294 | if (cbToRead)
|
---|
295 | {
|
---|
296 | if (cbToRead > cbScratchBuf)
|
---|
297 | {
|
---|
298 | pvDataRead = RTMemAlloc(cbToRead);
|
---|
299 | if (!pvDataRead)
|
---|
300 | rc = VERR_NO_MEMORY;
|
---|
301 | }
|
---|
302 |
|
---|
303 | if (RT_LIKELY(RT_SUCCESS(rc)))
|
---|
304 | rc = RTFileRead(pFile->hFile, pvDataRead, cbToRead, &cbRead);
|
---|
305 | }
|
---|
306 | else
|
---|
307 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
308 | }
|
---|
309 | else
|
---|
310 | rc = VERR_NOT_FOUND;
|
---|
311 |
|
---|
312 | /* Report back in any case. */
|
---|
313 | int rc2 = VbglR3GuestCtrlFileCbRead(pHostCtx, rc, pvDataRead, (uint32_t)cbRead);
|
---|
314 | if ( cbToRead > cbScratchBuf
|
---|
315 | && pvDataRead)
|
---|
316 | RTMemFree(pvDataRead);
|
---|
317 |
|
---|
318 | if (RT_FAILURE(rc2))
|
---|
319 | VGSvcError("Failed to report file read status, rc=%Rrc\n", rc2);
|
---|
320 | if (RT_SUCCESS(rc))
|
---|
321 | rc = rc2;
|
---|
322 | }
|
---|
323 |
|
---|
324 | #ifdef DEBUG
|
---|
325 | VGSvcVerbose(4, "Reading file '%s' (handle=%RU32) returned rc=%Rrc\n", pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
326 | #endif
|
---|
327 | return rc;
|
---|
328 | }
|
---|
329 |
|
---|
330 |
|
---|
331 | static int vgsvcGstCtrlSessionHandleFileReadAt(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
332 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
333 | {
|
---|
334 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
335 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
336 |
|
---|
337 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
338 |
|
---|
339 | uint32_t uHandle = 0;
|
---|
340 | uint32_t cbToRead;
|
---|
341 | uint64_t offReadAt;
|
---|
342 | int rc = VbglR3GuestCtrlFileGetReadAt(pHostCtx, &uHandle, &cbToRead, &offReadAt);
|
---|
343 | if (RT_SUCCESS(rc))
|
---|
344 | {
|
---|
345 | void *pvDataRead = pvScratchBuf;
|
---|
346 | size_t cbRead = 0;
|
---|
347 |
|
---|
348 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
349 | if (pFile)
|
---|
350 | {
|
---|
351 | if (cbToRead)
|
---|
352 | {
|
---|
353 | if (cbToRead > cbScratchBuf)
|
---|
354 | {
|
---|
355 | pvDataRead = RTMemAlloc(cbToRead);
|
---|
356 | if (!pvDataRead)
|
---|
357 | rc = VERR_NO_MEMORY;
|
---|
358 | }
|
---|
359 |
|
---|
360 | if (RT_SUCCESS(rc))
|
---|
361 | rc = RTFileReadAt(pFile->hFile, (RTFOFF)offReadAt, pvDataRead, cbToRead, &cbRead);
|
---|
362 | }
|
---|
363 | else
|
---|
364 | rc = VERR_BUFFER_UNDERFLOW;
|
---|
365 | }
|
---|
366 | else
|
---|
367 | rc = VERR_NOT_FOUND;
|
---|
368 |
|
---|
369 | /* Report back in any case. */
|
---|
370 | int rc2 = VbglR3GuestCtrlFileCbRead(pHostCtx, rc, pvDataRead, (uint32_t)cbRead);
|
---|
371 | if ( cbToRead > cbScratchBuf
|
---|
372 | && pvDataRead)
|
---|
373 | RTMemFree(pvDataRead);
|
---|
374 |
|
---|
375 | if (RT_FAILURE(rc2))
|
---|
376 | VGSvcError("Failed to report file read status, rc=%Rrc\n", rc2);
|
---|
377 | if (RT_SUCCESS(rc))
|
---|
378 | rc = rc2;
|
---|
379 | }
|
---|
380 |
|
---|
381 | #ifdef DEBUG
|
---|
382 | VGSvcVerbose(4, "Reading file '%s' at offset (handle=%RU32) returned rc=%Rrc\n",
|
---|
383 | pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
384 | #endif
|
---|
385 | return rc;
|
---|
386 | }
|
---|
387 |
|
---|
388 |
|
---|
389 | static int vgsvcGstCtrlSessionHandleFileWrite(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
390 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
391 | {
|
---|
392 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
393 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
394 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
395 | AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
|
---|
396 |
|
---|
397 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
398 |
|
---|
399 | uint32_t uHandle = 0;
|
---|
400 | uint32_t cbToWrite;
|
---|
401 | int rc = VbglR3GuestCtrlFileGetWrite(pHostCtx, &uHandle, pvScratchBuf, (uint32_t)cbScratchBuf, &cbToWrite);
|
---|
402 | if (RT_SUCCESS(rc))
|
---|
403 | {
|
---|
404 | size_t cbWritten = 0;
|
---|
405 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
406 | if (pFile)
|
---|
407 | {
|
---|
408 | rc = RTFileWrite(pFile->hFile, pvScratchBuf, cbToWrite, &cbWritten);
|
---|
409 | #ifdef DEBUG
|
---|
410 | VGSvcVerbose(4, "[File %s]: Writing pvScratchBuf=%p, cbToWrite=%RU32, cbWritten=%zu, rc=%Rrc\n",
|
---|
411 | pFile->szName, pvScratchBuf, cbToWrite, cbWritten, rc);
|
---|
412 | #endif
|
---|
413 | }
|
---|
414 | else
|
---|
415 | rc = VERR_NOT_FOUND;
|
---|
416 |
|
---|
417 | /* Report back in any case. */
|
---|
418 | int rc2 = VbglR3GuestCtrlFileCbWrite(pHostCtx, rc, (uint32_t)cbWritten);
|
---|
419 | if (RT_FAILURE(rc2))
|
---|
420 | VGSvcError("Failed to report file write status, rc=%Rrc\n", rc2);
|
---|
421 | if (RT_SUCCESS(rc))
|
---|
422 | rc = rc2;
|
---|
423 | }
|
---|
424 |
|
---|
425 | #ifdef DEBUG
|
---|
426 | VGSvcVerbose(4, "Writing file '%s' (handle=%RU32) returned rc=%Rrc\n", pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
427 | #endif
|
---|
428 | return rc;
|
---|
429 | }
|
---|
430 |
|
---|
431 |
|
---|
432 | static int vgsvcGstCtrlSessionHandleFileWriteAt(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
433 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
434 | {
|
---|
435 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
436 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
437 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
438 | AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
|
---|
439 |
|
---|
440 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
441 |
|
---|
442 | uint32_t uHandle = 0;
|
---|
443 | uint32_t cbToWrite;
|
---|
444 | uint64_t offWriteAt;
|
---|
445 |
|
---|
446 | int rc = VbglR3GuestCtrlFileGetWriteAt(pHostCtx, &uHandle, pvScratchBuf, (uint32_t)cbScratchBuf, &cbToWrite, &offWriteAt);
|
---|
447 | if (RT_SUCCESS(rc))
|
---|
448 | {
|
---|
449 | size_t cbWritten = 0;
|
---|
450 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
451 | if (pFile)
|
---|
452 | {
|
---|
453 | rc = RTFileWriteAt(pFile->hFile, (RTFOFF)offWriteAt, pvScratchBuf, cbToWrite, &cbWritten);
|
---|
454 | #ifdef DEBUG
|
---|
455 | VGSvcVerbose(4, "[File %s]: Writing offWriteAt=%RI64, pvScratchBuf=%p, cbToWrite=%RU32, cbWritten=%zu, rc=%Rrc\n",
|
---|
456 | pFile->szName, offWriteAt, pvScratchBuf, cbToWrite, cbWritten, rc);
|
---|
457 | #endif
|
---|
458 | }
|
---|
459 | else
|
---|
460 | rc = VERR_NOT_FOUND;
|
---|
461 |
|
---|
462 | /* Report back in any case. */
|
---|
463 | int rc2 = VbglR3GuestCtrlFileCbWrite(pHostCtx, rc, (uint32_t)cbWritten);
|
---|
464 | if (RT_FAILURE(rc2))
|
---|
465 | VGSvcError("Failed to report file write status, rc=%Rrc\n", rc2);
|
---|
466 | if (RT_SUCCESS(rc))
|
---|
467 | rc = rc2;
|
---|
468 | }
|
---|
469 |
|
---|
470 | #ifdef DEBUG
|
---|
471 | VGSvcVerbose(4, "Writing file '%s' at offset (handle=%RU32) returned rc=%Rrc\n",
|
---|
472 | pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
473 | #endif
|
---|
474 | return rc;
|
---|
475 | }
|
---|
476 |
|
---|
477 |
|
---|
478 | static int vgsvcGstCtrlSessionHandleFileSeek(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
479 | {
|
---|
480 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
481 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
482 |
|
---|
483 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
484 |
|
---|
485 | uint32_t uHandle = 0;
|
---|
486 | uint32_t uSeekMethod;
|
---|
487 | uint64_t offSeek; /* Will be converted to int64_t. */
|
---|
488 | int rc = VbglR3GuestCtrlFileGetSeek(pHostCtx, &uHandle, &uSeekMethod, &offSeek);
|
---|
489 | if (RT_SUCCESS(rc))
|
---|
490 | {
|
---|
491 | uint64_t offActual = 0;
|
---|
492 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
493 | if (pFile)
|
---|
494 | {
|
---|
495 | unsigned uSeekMethodIprt;
|
---|
496 | switch (uSeekMethod)
|
---|
497 | {
|
---|
498 | case GUEST_FILE_SEEKTYPE_BEGIN:
|
---|
499 | uSeekMethodIprt = RTFILE_SEEK_BEGIN;
|
---|
500 | break;
|
---|
501 |
|
---|
502 | case GUEST_FILE_SEEKTYPE_CURRENT:
|
---|
503 | uSeekMethodIprt = RTFILE_SEEK_CURRENT;
|
---|
504 | break;
|
---|
505 |
|
---|
506 | case GUEST_FILE_SEEKTYPE_END:
|
---|
507 | uSeekMethodIprt = RTFILE_SEEK_END;
|
---|
508 | break;
|
---|
509 |
|
---|
510 | default:
|
---|
511 | rc = VERR_NOT_SUPPORTED;
|
---|
512 | uSeekMethodIprt = RTFILE_SEEK_BEGIN; /* Shut up MSC */
|
---|
513 | break;
|
---|
514 | }
|
---|
515 |
|
---|
516 | if (RT_SUCCESS(rc))
|
---|
517 | {
|
---|
518 | rc = RTFileSeek(pFile->hFile, (int64_t)offSeek, uSeekMethodIprt, &offActual);
|
---|
519 | #ifdef DEBUG
|
---|
520 | VGSvcVerbose(4, "[File %s]: Seeking to offSeek=%RI64, uSeekMethodIPRT=%RU16, rc=%Rrc\n",
|
---|
521 | pFile->szName, offSeek, uSeekMethodIprt, rc);
|
---|
522 | #endif
|
---|
523 | }
|
---|
524 | }
|
---|
525 | else
|
---|
526 | rc = VERR_NOT_FOUND;
|
---|
527 |
|
---|
528 | /* Report back in any case. */
|
---|
529 | int rc2 = VbglR3GuestCtrlFileCbSeek(pHostCtx, rc, offActual);
|
---|
530 | if (RT_FAILURE(rc2))
|
---|
531 | VGSvcError("Failed to report file seek status, rc=%Rrc\n", rc2);
|
---|
532 | if (RT_SUCCESS(rc))
|
---|
533 | rc = rc2;
|
---|
534 | }
|
---|
535 |
|
---|
536 | #ifdef DEBUG
|
---|
537 | VGSvcVerbose(4, "Seeking file '%s' (handle=%RU32) returned rc=%Rrc\n", pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
538 | #endif
|
---|
539 | return rc;
|
---|
540 | }
|
---|
541 |
|
---|
542 |
|
---|
543 | static int vgsvcGstCtrlSessionHandleFileTell(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
544 | {
|
---|
545 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
546 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
547 |
|
---|
548 | PVBOXSERVICECTRLFILE pFile = NULL;
|
---|
549 |
|
---|
550 | uint32_t uHandle = 0;
|
---|
551 | int rc = VbglR3GuestCtrlFileGetTell(pHostCtx, &uHandle);
|
---|
552 | if (RT_SUCCESS(rc))
|
---|
553 | {
|
---|
554 | uint64_t off = 0;
|
---|
555 | pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
556 | if (pFile)
|
---|
557 | {
|
---|
558 | off = RTFileTell(pFile->hFile);
|
---|
559 | #ifdef DEBUG
|
---|
560 | VGSvcVerbose(4, "[File %s]: Telling off=%RU64\n", pFile->szName, off);
|
---|
561 | #endif
|
---|
562 | }
|
---|
563 | else
|
---|
564 | rc = VERR_NOT_FOUND;
|
---|
565 |
|
---|
566 | /* Report back in any case. */
|
---|
567 | int rc2 = VbglR3GuestCtrlFileCbTell(pHostCtx, rc, off);
|
---|
568 | if (RT_FAILURE(rc2))
|
---|
569 | VGSvcError("Failed to report file tell status, rc=%Rrc\n", rc2);
|
---|
570 | if (RT_SUCCESS(rc))
|
---|
571 | rc = rc2;
|
---|
572 | }
|
---|
573 |
|
---|
574 | #ifdef DEBUG
|
---|
575 | VGSvcVerbose(4, "Telling file '%s' (handle=%RU32) returned rc=%Rrc\n", pFile ? pFile->szName : "<Not found>", uHandle, rc);
|
---|
576 | #endif
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 |
|
---|
580 |
|
---|
581 | static int vgsvcGstCtrlSessionHandlePathRename(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
582 | {
|
---|
583 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
584 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
585 |
|
---|
586 | char szSource[RTPATH_MAX];
|
---|
587 | char szDest[RTPATH_MAX];
|
---|
588 | uint32_t fFlags = 0;
|
---|
589 |
|
---|
590 | int rc = VbglR3GuestCtrlPathGetRename(pHostCtx,
|
---|
591 | szSource, sizeof(szSource),
|
---|
592 | szDest, sizeof(szDest),
|
---|
593 | /* Flags of type PATHRENAME_FLAG_. */
|
---|
594 | &fFlags);
|
---|
595 | if (RT_SUCCESS(rc))
|
---|
596 | {
|
---|
597 | if (fFlags & ~PATHRENAME_FLAG_VALID_MASK)
|
---|
598 | rc = VERR_NOT_SUPPORTED;
|
---|
599 |
|
---|
600 | VGSvcVerbose(4, "Renaming '%s' to '%s', fFlags=0x%x, rc=%Rrc\n", szSource, szDest, fFlags, rc);
|
---|
601 |
|
---|
602 | if (RT_SUCCESS(rc))
|
---|
603 | {
|
---|
604 | /** @todo r=bird: shouldn't you use a different variable here for the IPRT flags??? */
|
---|
605 | if (fFlags & PATHRENAME_FLAG_NO_REPLACE)
|
---|
606 | fFlags |= RTPATHRENAME_FLAGS_NO_REPLACE;
|
---|
607 |
|
---|
608 | if (fFlags & PATHRENAME_FLAG_REPLACE)
|
---|
609 | fFlags |= RTPATHRENAME_FLAGS_REPLACE;
|
---|
610 |
|
---|
611 | if (fFlags & PATHRENAME_FLAG_NO_SYMLINKS)
|
---|
612 | fFlags |= RTPATHRENAME_FLAGS_NO_SYMLINKS;
|
---|
613 |
|
---|
614 | rc = RTPathRename(szSource, szDest, fFlags);
|
---|
615 | }
|
---|
616 |
|
---|
617 | /* Report back in any case. */
|
---|
618 | int rc2 = VbglR3GuestCtrlMsgReply(pHostCtx, rc);
|
---|
619 | if (RT_FAILURE(rc2))
|
---|
620 | VGSvcError("Failed to report renaming status, rc=%Rrc\n", rc2);
|
---|
621 | if (RT_SUCCESS(rc))
|
---|
622 | rc = rc2;
|
---|
623 | }
|
---|
624 |
|
---|
625 | #ifdef DEBUG
|
---|
626 | VGSvcVerbose(4, "Renaming '%s' to '%s' returned rc=%Rrc\n", szSource, szDest, rc);
|
---|
627 | #endif
|
---|
628 | return rc;
|
---|
629 | }
|
---|
630 |
|
---|
631 |
|
---|
632 | /**
|
---|
633 | * Handles starting a guest processes.
|
---|
634 | *
|
---|
635 | * @returns VBox status code.
|
---|
636 | * @param pSession Guest session.
|
---|
637 | * @param pHostCtx Host context.
|
---|
638 | */
|
---|
639 | static int vgsvcGstCtrlSessionHandleProcExec(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
640 | {
|
---|
641 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
642 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
643 |
|
---|
644 | int rc = VINF_SUCCESS;
|
---|
645 | bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
|
---|
646 |
|
---|
647 | switch (pHostCtx->uProtocol)
|
---|
648 | {
|
---|
649 | case 1: /* Guest Additions < 4.3. */
|
---|
650 | if (pHostCtx->uNumParms != 11)
|
---|
651 | rc = VERR_NOT_SUPPORTED;
|
---|
652 | break;
|
---|
653 |
|
---|
654 | case 2: /* Guest Additions >= 4.3. */
|
---|
655 | if (pHostCtx->uNumParms != 12)
|
---|
656 | rc = VERR_NOT_SUPPORTED;
|
---|
657 | break;
|
---|
658 |
|
---|
659 | default:
|
---|
660 | rc = VERR_NOT_SUPPORTED;
|
---|
661 | break;
|
---|
662 | }
|
---|
663 |
|
---|
664 | if (RT_SUCCESS(rc))
|
---|
665 | {
|
---|
666 | VBOXSERVICECTRLPROCSTARTUPINFO startupInfo;
|
---|
667 | RT_ZERO(startupInfo);
|
---|
668 |
|
---|
669 | /* Initialize maximum environment block size -- needed as input
|
---|
670 | * parameter to retrieve the stuff from the host. On output this then
|
---|
671 | * will contain the actual block size. */
|
---|
672 | startupInfo.cbEnv = sizeof(startupInfo.szEnv);
|
---|
673 |
|
---|
674 | rc = VbglR3GuestCtrlProcGetStart(pHostCtx,
|
---|
675 | /* Command */
|
---|
676 | startupInfo.szCmd, sizeof(startupInfo.szCmd),
|
---|
677 | /* Flags */
|
---|
678 | &startupInfo.uFlags,
|
---|
679 | /* Arguments */
|
---|
680 | startupInfo.szArgs, sizeof(startupInfo.szArgs), &startupInfo.uNumArgs,
|
---|
681 | /* Environment */
|
---|
682 | startupInfo.szEnv, &startupInfo.cbEnv, &startupInfo.uNumEnvVars,
|
---|
683 | /* Credentials; for hosts with VBox < 4.3 (protocol version 1).
|
---|
684 | * For protocl v2 and up the credentials are part of the session
|
---|
685 | * opening call. */
|
---|
686 | startupInfo.szUser, sizeof(startupInfo.szUser),
|
---|
687 | startupInfo.szPassword, sizeof(startupInfo.szPassword),
|
---|
688 | /* Timeout (in ms) */
|
---|
689 | &startupInfo.uTimeLimitMS,
|
---|
690 | /* Process priority */
|
---|
691 | &startupInfo.uPriority,
|
---|
692 | /* Process affinity */
|
---|
693 | startupInfo.uAffinity, sizeof(startupInfo.uAffinity), &startupInfo.uNumAffinity);
|
---|
694 | if (RT_SUCCESS(rc))
|
---|
695 | {
|
---|
696 | VGSvcVerbose(3, "Request to start process szCmd=%s, fFlags=0x%x, szArgs=%s, szEnv=%s, uTimeout=%RU32\n",
|
---|
697 | startupInfo.szCmd, startupInfo.uFlags,
|
---|
698 | startupInfo.uNumArgs ? startupInfo.szArgs : "<None>",
|
---|
699 | startupInfo.uNumEnvVars ? startupInfo.szEnv : "<None>",
|
---|
700 | startupInfo.uTimeLimitMS);
|
---|
701 |
|
---|
702 | rc = VGSvcGstCtrlSessionProcessStartAllowed(pSession, &fStartAllowed);
|
---|
703 | if (RT_SUCCESS(rc))
|
---|
704 | {
|
---|
705 | if (fStartAllowed)
|
---|
706 | rc = VGSvcGstCtrlProcessStart(pSession, &startupInfo, pHostCtx->uContextID);
|
---|
707 | else
|
---|
708 | rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
|
---|
709 | }
|
---|
710 | }
|
---|
711 | }
|
---|
712 |
|
---|
713 | /* In case of an error we need to notify the host to not wait forever for our response. */
|
---|
714 | if (RT_FAILURE(rc))
|
---|
715 | {
|
---|
716 | VGSvcError("Starting process failed with rc=%Rrc, protocol=%RU32, parameters=%RU32\n",
|
---|
717 | rc, pHostCtx->uProtocol, pHostCtx->uNumParms);
|
---|
718 |
|
---|
719 | /* Don't report back if we didn't supply sufficient buffer for getting
|
---|
720 | * the actual command -- we don't have the matching context ID. */
|
---|
721 | if (rc != VERR_TOO_MUCH_DATA)
|
---|
722 | {
|
---|
723 | /*
|
---|
724 | * Note: The context ID can be 0 because we mabye weren't able to fetch the command
|
---|
725 | * from the host. The host in case has to deal with that!
|
---|
726 | */
|
---|
727 | int rc2 = VbglR3GuestCtrlProcCbStatus(pHostCtx, 0 /* PID, invalid */,
|
---|
728 | PROC_STS_ERROR, rc,
|
---|
729 | NULL /* pvData */, 0 /* cbData */);
|
---|
730 | if (RT_FAILURE(rc2))
|
---|
731 | VGSvcError("Error sending start process status to host, rc=%Rrc\n", rc2);
|
---|
732 | }
|
---|
733 | }
|
---|
734 |
|
---|
735 | return rc;
|
---|
736 | }
|
---|
737 |
|
---|
738 |
|
---|
739 | /**
|
---|
740 | * Sends stdin input to a specific guest process.
|
---|
741 | *
|
---|
742 | * @returns VBox status code.
|
---|
743 | * @param pSession The session which is in charge.
|
---|
744 | * @param pHostCtx The host context to use.
|
---|
745 | * @param pvScratchBuf The scratch buffer.
|
---|
746 | * @param cbScratchBuf The scratch buffer size for retrieving the input
|
---|
747 | * data.
|
---|
748 | */
|
---|
749 | static int vgsvcGstCtrlSessionHandleProcInput(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
750 | void *pvScratchBuf, size_t cbScratchBuf)
|
---|
751 | {
|
---|
752 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
753 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
754 | AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
|
---|
755 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
756 |
|
---|
757 | uint32_t uPID;
|
---|
758 | uint32_t fFlags;
|
---|
759 | uint32_t cbSize;
|
---|
760 |
|
---|
761 | #if 0 /* unused */
|
---|
762 | uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
|
---|
763 | uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
|
---|
764 | #endif
|
---|
765 |
|
---|
766 | /*
|
---|
767 | * Ask the host for the input data.
|
---|
768 | */
|
---|
769 | int rc = VbglR3GuestCtrlProcGetInput(pHostCtx, &uPID, &fFlags,
|
---|
770 | pvScratchBuf, (uint32_t)cbScratchBuf, &cbSize);
|
---|
771 | if (RT_FAILURE(rc))
|
---|
772 | VGSvcError("Failed to retrieve process input command for PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
773 | else if (cbSize > cbScratchBuf)
|
---|
774 | {
|
---|
775 | VGSvcError("Too much process input received, rejecting: uPID=%RU32, cbSize=%RU32, cbScratchBuf=%RU32\n",
|
---|
776 | uPID, cbSize, cbScratchBuf);
|
---|
777 | rc = VERR_TOO_MUCH_DATA;
|
---|
778 | }
|
---|
779 | else
|
---|
780 | {
|
---|
781 | /*
|
---|
782 | * Is this the last input block we need to deliver? Then let the pipe know ...
|
---|
783 | */
|
---|
784 | bool fPendingClose = false;
|
---|
785 | if (fFlags & INPUT_FLAG_EOF)
|
---|
786 | {
|
---|
787 | fPendingClose = true;
|
---|
788 | #ifdef DEBUG
|
---|
789 | VGSvcVerbose(4, "Got last process input block for PID=%RU32 (%RU32 bytes) ...\n", uPID, cbSize);
|
---|
790 | #endif
|
---|
791 | }
|
---|
792 |
|
---|
793 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
794 | if (pProcess)
|
---|
795 | {
|
---|
796 | rc = VGSvcGstCtrlProcessHandleInput(pProcess, pHostCtx, fPendingClose, pvScratchBuf, cbSize);
|
---|
797 | if (RT_FAILURE(rc))
|
---|
798 | VGSvcError("Error handling input command for PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
799 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
800 | }
|
---|
801 | else
|
---|
802 | rc = VERR_NOT_FOUND;
|
---|
803 | }
|
---|
804 |
|
---|
805 | #ifdef DEBUG
|
---|
806 | VGSvcVerbose(4, "Setting input for PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
807 | #endif
|
---|
808 | return rc;
|
---|
809 | }
|
---|
810 |
|
---|
811 |
|
---|
812 | /**
|
---|
813 | * Gets stdout/stderr output of a specific guest process.
|
---|
814 | *
|
---|
815 | * @returns VBox status code.
|
---|
816 | * @param pSession The session which is in charge.
|
---|
817 | * @param pHostCtx The host context to use.
|
---|
818 | */
|
---|
819 | static int vgsvcGstCtrlSessionHandleProcOutput(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
820 | {
|
---|
821 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
822 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
823 |
|
---|
824 | uint32_t uPID;
|
---|
825 | uint32_t uHandleID;
|
---|
826 | uint32_t fFlags;
|
---|
827 |
|
---|
828 | int rc = VbglR3GuestCtrlProcGetOutput(pHostCtx, &uPID, &uHandleID, &fFlags);
|
---|
829 | #ifdef DEBUG_andy
|
---|
830 | VGSvcVerbose(4, "Getting output for PID=%RU32, CID=%RU32, uHandleID=%RU32, fFlags=%RU32\n",
|
---|
831 | uPID, pHostCtx->uContextID, uHandleID, fFlags);
|
---|
832 | #endif
|
---|
833 | if (RT_SUCCESS(rc))
|
---|
834 | {
|
---|
835 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
836 | if (pProcess)
|
---|
837 | {
|
---|
838 | rc = VGSvcGstCtrlProcessHandleOutput(pProcess, pHostCtx, uHandleID, _64K /* cbToRead */, fFlags);
|
---|
839 | if (RT_FAILURE(rc))
|
---|
840 | VGSvcError("Error getting output for PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
841 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
842 | }
|
---|
843 | else
|
---|
844 | rc = VERR_NOT_FOUND;
|
---|
845 | }
|
---|
846 |
|
---|
847 | #ifdef DEBUG_andy
|
---|
848 | VGSvcVerbose(4, "Getting output for PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
849 | #endif
|
---|
850 | return rc;
|
---|
851 | }
|
---|
852 |
|
---|
853 |
|
---|
854 | /**
|
---|
855 | * Tells a guest process to terminate.
|
---|
856 | *
|
---|
857 | * @returns VBox status code.
|
---|
858 | * @param pSession The session which is in charge.
|
---|
859 | * @param pHostCtx The host context to use.
|
---|
860 | */
|
---|
861 | static int vgsvcGstCtrlSessionHandleProcTerminate(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
862 | {
|
---|
863 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
864 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
865 |
|
---|
866 | uint32_t uPID;
|
---|
867 | int rc = VbglR3GuestCtrlProcGetTerminate(pHostCtx, &uPID);
|
---|
868 | if (RT_SUCCESS(rc))
|
---|
869 | {
|
---|
870 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
871 | if (pProcess)
|
---|
872 | {
|
---|
873 | rc = VGSvcGstCtrlProcessHandleTerm(pProcess);
|
---|
874 |
|
---|
875 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
876 | }
|
---|
877 | else
|
---|
878 | rc = VERR_NOT_FOUND;
|
---|
879 | }
|
---|
880 |
|
---|
881 | #ifdef DEBUG_andy
|
---|
882 | VGSvcVerbose(4, "Terminating PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
883 | #endif
|
---|
884 | return rc;
|
---|
885 | }
|
---|
886 |
|
---|
887 |
|
---|
888 | static int vgsvcGstCtrlSessionHandleProcWaitFor(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
889 | {
|
---|
890 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
891 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
892 |
|
---|
893 | uint32_t uPID;
|
---|
894 | uint32_t uWaitFlags; uint32_t uTimeoutMS;
|
---|
895 |
|
---|
896 | int rc = VbglR3GuestCtrlProcGetWaitFor(pHostCtx, &uPID, &uWaitFlags, &uTimeoutMS);
|
---|
897 | if (RT_SUCCESS(rc))
|
---|
898 | {
|
---|
899 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
900 | if (pProcess)
|
---|
901 | {
|
---|
902 | rc = VERR_NOT_IMPLEMENTED; /** @todo */
|
---|
903 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
904 | }
|
---|
905 | else
|
---|
906 | rc = VERR_NOT_FOUND;
|
---|
907 | }
|
---|
908 |
|
---|
909 | return rc;
|
---|
910 | }
|
---|
911 |
|
---|
912 |
|
---|
913 | int VGSvcGstCtrlSessionHandler(PVBOXSERVICECTRLSESSION pSession, uint32_t uMsg, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
914 | void *pvScratchBuf, size_t cbScratchBuf, volatile bool *pfShutdown)
|
---|
915 | {
|
---|
916 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
917 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
918 | AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
|
---|
919 | AssertPtrReturn(pfShutdown, VERR_INVALID_POINTER);
|
---|
920 |
|
---|
921 |
|
---|
922 | /*
|
---|
923 | * Only anonymous sessions (that is, sessions which run with local
|
---|
924 | * service privileges) or spawned session processes can do certain
|
---|
925 | * operations.
|
---|
926 | */
|
---|
927 | bool const fImpersonated = RT_BOOL(pSession->fFlags & ( VBOXSERVICECTRLSESSION_FLAG_SPAWN
|
---|
928 | | VBOXSERVICECTRLSESSION_FLAG_ANONYMOUS));
|
---|
929 | int rc;
|
---|
930 | switch (uMsg)
|
---|
931 | {
|
---|
932 | case HOST_SESSION_CLOSE:
|
---|
933 | /* Shutdown (this spawn). */
|
---|
934 | rc = VGSvcGstCtrlSessionClose(pSession);
|
---|
935 | *pfShutdown = true; /* Shutdown in any case. */
|
---|
936 | break;
|
---|
937 |
|
---|
938 | case HOST_DIR_REMOVE:
|
---|
939 | if (fImpersonated)
|
---|
940 | rc = vgsvcGstCtrlSessionHandleDirRemove(pSession, pHostCtx);
|
---|
941 | else
|
---|
942 | rc = VERR_NOT_SUPPORTED;
|
---|
943 | break;
|
---|
944 |
|
---|
945 | case HOST_EXEC_CMD:
|
---|
946 | rc = vgsvcGstCtrlSessionHandleProcExec(pSession, pHostCtx);
|
---|
947 | break;
|
---|
948 |
|
---|
949 | case HOST_EXEC_SET_INPUT:
|
---|
950 | rc = vgsvcGstCtrlSessionHandleProcInput(pSession, pHostCtx, pvScratchBuf, cbScratchBuf);
|
---|
951 | break;
|
---|
952 |
|
---|
953 | case HOST_EXEC_GET_OUTPUT:
|
---|
954 | rc = vgsvcGstCtrlSessionHandleProcOutput(pSession, pHostCtx);
|
---|
955 | break;
|
---|
956 |
|
---|
957 | case HOST_EXEC_TERMINATE:
|
---|
958 | rc = vgsvcGstCtrlSessionHandleProcTerminate(pSession, pHostCtx);
|
---|
959 | break;
|
---|
960 |
|
---|
961 | case HOST_EXEC_WAIT_FOR:
|
---|
962 | rc = vgsvcGstCtrlSessionHandleProcWaitFor(pSession, pHostCtx);
|
---|
963 | break;
|
---|
964 |
|
---|
965 | case HOST_FILE_OPEN:
|
---|
966 | if (fImpersonated)
|
---|
967 | rc = vgsvcGstCtrlSessionHandleFileOpen(pSession, pHostCtx);
|
---|
968 | else
|
---|
969 | rc = VERR_NOT_SUPPORTED;
|
---|
970 | break;
|
---|
971 |
|
---|
972 | case HOST_FILE_CLOSE:
|
---|
973 | if (fImpersonated)
|
---|
974 | rc = vgsvcGstCtrlSessionHandleFileClose(pSession, pHostCtx);
|
---|
975 | else
|
---|
976 | rc = VERR_NOT_SUPPORTED;
|
---|
977 | break;
|
---|
978 |
|
---|
979 | case HOST_FILE_READ:
|
---|
980 | if (fImpersonated)
|
---|
981 | rc = vgsvcGstCtrlSessionHandleFileRead(pSession, pHostCtx, pvScratchBuf, cbScratchBuf);
|
---|
982 | else
|
---|
983 | rc = VERR_NOT_SUPPORTED;
|
---|
984 | break;
|
---|
985 |
|
---|
986 | case HOST_FILE_READ_AT:
|
---|
987 | if (fImpersonated)
|
---|
988 | rc = vgsvcGstCtrlSessionHandleFileReadAt(pSession, pHostCtx, pvScratchBuf, cbScratchBuf);
|
---|
989 | else
|
---|
990 | rc = VERR_NOT_SUPPORTED;
|
---|
991 | break;
|
---|
992 |
|
---|
993 | case HOST_FILE_WRITE:
|
---|
994 | if (fImpersonated)
|
---|
995 | rc = vgsvcGstCtrlSessionHandleFileWrite(pSession, pHostCtx, pvScratchBuf, cbScratchBuf);
|
---|
996 | else
|
---|
997 | rc = VERR_NOT_SUPPORTED;
|
---|
998 | break;
|
---|
999 |
|
---|
1000 | case HOST_FILE_WRITE_AT:
|
---|
1001 | if (fImpersonated)
|
---|
1002 | rc = vgsvcGstCtrlSessionHandleFileWriteAt(pSession, pHostCtx, pvScratchBuf, cbScratchBuf);
|
---|
1003 | else
|
---|
1004 | rc = VERR_NOT_SUPPORTED;
|
---|
1005 | break;
|
---|
1006 |
|
---|
1007 | case HOST_FILE_SEEK:
|
---|
1008 | if (fImpersonated)
|
---|
1009 | rc = vgsvcGstCtrlSessionHandleFileSeek(pSession, pHostCtx);
|
---|
1010 | else
|
---|
1011 | rc = VERR_NOT_SUPPORTED;
|
---|
1012 | break;
|
---|
1013 |
|
---|
1014 | case HOST_FILE_TELL:
|
---|
1015 | if (fImpersonated)
|
---|
1016 | rc = vgsvcGstCtrlSessionHandleFileTell(pSession, pHostCtx);
|
---|
1017 | else
|
---|
1018 | rc = VERR_NOT_SUPPORTED;
|
---|
1019 | break;
|
---|
1020 |
|
---|
1021 | case HOST_PATH_RENAME:
|
---|
1022 | if (fImpersonated)
|
---|
1023 | rc = vgsvcGstCtrlSessionHandlePathRename(pSession, pHostCtx);
|
---|
1024 | else
|
---|
1025 | rc = VERR_NOT_SUPPORTED;
|
---|
1026 | break;
|
---|
1027 |
|
---|
1028 | default:
|
---|
1029 | rc = VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID);
|
---|
1030 | VGSvcVerbose(3, "Unsupported message (uMsg=%RU32, cParms=%RU32) from host, skipping\n", uMsg, pHostCtx->uNumParms);
|
---|
1031 | break;
|
---|
1032 | }
|
---|
1033 |
|
---|
1034 | if (RT_FAILURE(rc))
|
---|
1035 | VGSvcError("Error while handling message (uMsg=%RU32, cParms=%RU32), rc=%Rrc\n", uMsg, pHostCtx->uNumParms, rc);
|
---|
1036 |
|
---|
1037 | return rc;
|
---|
1038 | }
|
---|
1039 |
|
---|
1040 |
|
---|
1041 | /**
|
---|
1042 | * Thread main routine for a spawned guest session process.
|
---|
1043 | * This thread runs in the main executable to control the spawned session process.
|
---|
1044 | *
|
---|
1045 | * @returns VBox status code.
|
---|
1046 | * @param hThreadSelf Thread handle.
|
---|
1047 | * @param pvUser Pointer to a VBOXSERVICECTRLSESSIONTHREAD structure.
|
---|
1048 | *
|
---|
1049 | */
|
---|
1050 | static DECLCALLBACK(int) vgsvcGstCtrlSessionThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
1051 | {
|
---|
1052 | PVBOXSERVICECTRLSESSIONTHREAD pThread = (PVBOXSERVICECTRLSESSIONTHREAD)pvUser;
|
---|
1053 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
1054 |
|
---|
1055 | uint32_t uSessionID = pThread->StartupInfo.uSessionID;
|
---|
1056 |
|
---|
1057 | uint32_t uClientID;
|
---|
1058 | int rc = VbglR3GuestCtrlConnect(&uClientID);
|
---|
1059 | if (RT_SUCCESS(rc))
|
---|
1060 | {
|
---|
1061 | VGSvcVerbose(3, "Session ID=%RU32 thread running, client ID=%RU32\n", uSessionID, uClientID);
|
---|
1062 |
|
---|
1063 | /* The session thread is not interested in receiving any commands;
|
---|
1064 | * tell the host service. */
|
---|
1065 | rc = VbglR3GuestCtrlMsgFilterSet(uClientID, 0 /* Skip all */, 0 /* Filter mask to add */, 0 /* Filter mask to remove */);
|
---|
1066 | if (RT_FAILURE(rc))
|
---|
1067 | {
|
---|
1068 | VGSvcError("Unable to set message filter, rc=%Rrc\n", rc);
|
---|
1069 | /* Non-critical. */
|
---|
1070 | rc = VINF_SUCCESS;
|
---|
1071 | }
|
---|
1072 | }
|
---|
1073 | else
|
---|
1074 | VGSvcError("Error connecting to guest control service, rc=%Rrc\n", rc);
|
---|
1075 |
|
---|
1076 | if (RT_FAILURE(rc))
|
---|
1077 | pThread->fShutdown = true;
|
---|
1078 |
|
---|
1079 | /* Let caller know that we're done initializing, regardless of the result. */
|
---|
1080 | int rc2 = RTThreadUserSignal(hThreadSelf);
|
---|
1081 | AssertRC(rc2);
|
---|
1082 |
|
---|
1083 | if (RT_FAILURE(rc))
|
---|
1084 | return rc;
|
---|
1085 |
|
---|
1086 | bool fProcessAlive = true;
|
---|
1087 | RTPROCSTATUS ProcessStatus;
|
---|
1088 | RT_ZERO(ProcessStatus);
|
---|
1089 |
|
---|
1090 | int rcWait;
|
---|
1091 | uint32_t uTimeoutsMS = 30 * 1000; /** @todo Make this configurable. Later. */
|
---|
1092 | uint64_t u64TimeoutStart = 0;
|
---|
1093 |
|
---|
1094 | for (;;)
|
---|
1095 | {
|
---|
1096 | rcWait = RTProcWaitNoResume(pThread->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
|
---|
1097 | if (RT_UNLIKELY(rcWait == VERR_INTERRUPTED))
|
---|
1098 | continue;
|
---|
1099 |
|
---|
1100 | if ( rcWait == VINF_SUCCESS
|
---|
1101 | || rcWait == VERR_PROCESS_NOT_FOUND)
|
---|
1102 | {
|
---|
1103 | fProcessAlive = false;
|
---|
1104 | break;
|
---|
1105 | }
|
---|
1106 | AssertMsgBreak(rcWait == VERR_PROCESS_RUNNING,
|
---|
1107 | ("Got unexpected rc=%Rrc while waiting for session process termination\n", rcWait));
|
---|
1108 |
|
---|
1109 | if (ASMAtomicReadBool(&pThread->fShutdown))
|
---|
1110 | {
|
---|
1111 | if (!u64TimeoutStart)
|
---|
1112 | {
|
---|
1113 | VGSvcVerbose(3, "Notifying guest session process (PID=%RU32, session ID=%RU32) ...\n",
|
---|
1114 | pThread->hProcess, uSessionID);
|
---|
1115 |
|
---|
1116 | VBGLR3GUESTCTRLCMDCTX hostCtx =
|
---|
1117 | {
|
---|
1118 | /* .idClient = */ uClientID,
|
---|
1119 | /* .idContext = */ VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(uSessionID),
|
---|
1120 | /* .uProtocol = */ pThread->StartupInfo.uProtocol,
|
---|
1121 | /* .cParams = */ 2
|
---|
1122 | };
|
---|
1123 | rc = VbglR3GuestCtrlSessionClose(&hostCtx, 0 /* fFlags */);
|
---|
1124 | if (RT_FAILURE(rc))
|
---|
1125 | {
|
---|
1126 | VGSvcError("Unable to notify guest session process (PID=%RU32, session ID=%RU32), rc=%Rrc\n",
|
---|
1127 | pThread->hProcess, uSessionID, rc);
|
---|
1128 |
|
---|
1129 | if (rc == VERR_NOT_SUPPORTED)
|
---|
1130 | {
|
---|
1131 | /* Terminate guest session process in case it's not supported by a too old host. */
|
---|
1132 | rc = RTProcTerminate(pThread->hProcess);
|
---|
1133 | VGSvcVerbose(3, "Terminating guest session process (PID=%RU32) ended with rc=%Rrc\n",
|
---|
1134 | pThread->hProcess, rc);
|
---|
1135 | }
|
---|
1136 | break;
|
---|
1137 | }
|
---|
1138 |
|
---|
1139 | VGSvcVerbose(3, "Guest session ID=%RU32 thread was asked to terminate, waiting for session process to exit (%RU32ms timeout) ...\n",
|
---|
1140 | uSessionID, uTimeoutsMS);
|
---|
1141 | u64TimeoutStart = RTTimeMilliTS();
|
---|
1142 | continue; /* Don't waste time on waiting. */
|
---|
1143 | }
|
---|
1144 | if (RTTimeMilliTS() - u64TimeoutStart > uTimeoutsMS)
|
---|
1145 | {
|
---|
1146 | VGSvcVerbose(3, "Guest session ID=%RU32 process did not shut down within time\n", uSessionID);
|
---|
1147 | break;
|
---|
1148 | }
|
---|
1149 | }
|
---|
1150 |
|
---|
1151 | RTThreadSleep(100); /* Wait a bit. */
|
---|
1152 | }
|
---|
1153 |
|
---|
1154 | if (!fProcessAlive)
|
---|
1155 | {
|
---|
1156 | VGSvcVerbose(2, "Guest session process (ID=%RU32) terminated with rc=%Rrc, reason=%d, status=%d\n",
|
---|
1157 | uSessionID, rcWait, ProcessStatus.enmReason, ProcessStatus.iStatus);
|
---|
1158 | if (ProcessStatus.iStatus == RTEXITCODE_INIT)
|
---|
1159 | {
|
---|
1160 | VGSvcError("Guest session process (ID=%RU32) failed to initialize. Here some hints:\n", uSessionID);
|
---|
1161 | VGSvcError("- Is logging enabled and the output directory is read-only by the guest session user?\n");
|
---|
1162 | /** @todo Add more here. */
|
---|
1163 | }
|
---|
1164 | }
|
---|
1165 |
|
---|
1166 | uint32_t uSessionStatus = GUEST_SESSION_NOTIFYTYPE_UNDEFINED;
|
---|
1167 | uint32_t uSessionRc = VINF_SUCCESS; /** uint32_t vs. int. */
|
---|
1168 |
|
---|
1169 | if (fProcessAlive)
|
---|
1170 | {
|
---|
1171 | for (int i = 0; i < 3; i++)
|
---|
1172 | {
|
---|
1173 | VGSvcVerbose(2, "Guest session ID=%RU32 process still alive, killing attempt %d/3\n", uSessionID, i + 1);
|
---|
1174 |
|
---|
1175 | rc = RTProcTerminate(pThread->hProcess);
|
---|
1176 | if (RT_SUCCESS(rc))
|
---|
1177 | break;
|
---|
1178 | /** @todo r=bird: What's the point of sleeping 3 second after the last attempt? */
|
---|
1179 | RTThreadSleep(3000);
|
---|
1180 | }
|
---|
1181 |
|
---|
1182 | VGSvcVerbose(2, "Guest session ID=%RU32 process termination resulted in rc=%Rrc\n", uSessionID, rc);
|
---|
1183 |
|
---|
1184 | uSessionStatus = RT_SUCCESS(rc) ? GUEST_SESSION_NOTIFYTYPE_TOK : GUEST_SESSION_NOTIFYTYPE_TOA;
|
---|
1185 | }
|
---|
1186 | else if (RT_SUCCESS(rcWait))
|
---|
1187 | {
|
---|
1188 | switch (ProcessStatus.enmReason)
|
---|
1189 | {
|
---|
1190 | case RTPROCEXITREASON_NORMAL:
|
---|
1191 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEN;
|
---|
1192 | break;
|
---|
1193 |
|
---|
1194 | case RTPROCEXITREASON_ABEND:
|
---|
1195 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEA;
|
---|
1196 | break;
|
---|
1197 |
|
---|
1198 | case RTPROCEXITREASON_SIGNAL:
|
---|
1199 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TES;
|
---|
1200 | break;
|
---|
1201 |
|
---|
1202 | default:
|
---|
1203 | AssertMsgFailed(("Unhandled process termination reason (%d)\n", ProcessStatus.enmReason));
|
---|
1204 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEA;
|
---|
1205 | break;
|
---|
1206 | }
|
---|
1207 | }
|
---|
1208 | else
|
---|
1209 | {
|
---|
1210 | /* If we didn't find the guest process anymore, just assume it
|
---|
1211 | * terminated normally. */
|
---|
1212 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEN;
|
---|
1213 | }
|
---|
1214 |
|
---|
1215 | VGSvcVerbose(3, "Guest session ID=%RU32 thread ended with sessionStatus=%RU32, sessionRc=%Rrc\n",
|
---|
1216 | uSessionID, uSessionStatus, uSessionRc);
|
---|
1217 |
|
---|
1218 | /* Report final status. */
|
---|
1219 | Assert(uSessionStatus != GUEST_SESSION_NOTIFYTYPE_UNDEFINED);
|
---|
1220 | VBGLR3GUESTCTRLCMDCTX ctx = { uClientID, VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(uSessionID) };
|
---|
1221 | rc2 = VbglR3GuestCtrlSessionNotify(&ctx, uSessionStatus, uSessionRc);
|
---|
1222 | if (RT_FAILURE(rc2))
|
---|
1223 | VGSvcError("Reporting session ID=%RU32 final status failed with rc=%Rrc\n", uSessionID, rc2);
|
---|
1224 |
|
---|
1225 | VbglR3GuestCtrlDisconnect(uClientID);
|
---|
1226 |
|
---|
1227 | VGSvcVerbose(3, "Session ID=%RU32 thread ended with rc=%Rrc\n", uSessionID, rc);
|
---|
1228 | return rc;
|
---|
1229 | }
|
---|
1230 |
|
---|
1231 |
|
---|
1232 | static RTEXITCODE vgsvcGstCtrlSessionSpawnWorker(PVBOXSERVICECTRLSESSION pSession)
|
---|
1233 | {
|
---|
1234 | AssertPtrReturn(pSession, RTEXITCODE_FAILURE);
|
---|
1235 |
|
---|
1236 | bool fSessionFilter = true;
|
---|
1237 |
|
---|
1238 | VGSvcVerbose(0, "Hi, this is guest session ID=%RU32\n", pSession->StartupInfo.uSessionID);
|
---|
1239 |
|
---|
1240 | uint32_t uClientID;
|
---|
1241 | int rc = VbglR3GuestCtrlConnect(&uClientID);
|
---|
1242 | if (RT_SUCCESS(rc))
|
---|
1243 | {
|
---|
1244 | /* Set session filter. This prevents the guest control
|
---|
1245 | * host service to send messages which belong to another
|
---|
1246 | * session we don't want to handle. */
|
---|
1247 | uint32_t uFilterAdd = VBOX_GUESTCTRL_FILTER_BY_SESSION(pSession->StartupInfo.uSessionID);
|
---|
1248 | rc = VbglR3GuestCtrlMsgFilterSet(uClientID,
|
---|
1249 | VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(pSession->StartupInfo.uSessionID),
|
---|
1250 | uFilterAdd, 0 /* Filter remove */);
|
---|
1251 | VGSvcVerbose(3, "Setting message filterAdd=0x%x returned %Rrc\n", uFilterAdd, rc);
|
---|
1252 |
|
---|
1253 | if ( RT_FAILURE(rc)
|
---|
1254 | && rc == VERR_NOT_SUPPORTED)
|
---|
1255 | {
|
---|
1256 | /* No session filter available. Skip. */
|
---|
1257 | fSessionFilter = false;
|
---|
1258 |
|
---|
1259 | rc = VINF_SUCCESS;
|
---|
1260 | }
|
---|
1261 |
|
---|
1262 | VGSvcVerbose(1, "Using client ID=%RU32\n", uClientID);
|
---|
1263 | }
|
---|
1264 | else
|
---|
1265 | VGSvcError("Error connecting to guest control service, rc=%Rrc\n", rc);
|
---|
1266 |
|
---|
1267 | /* Report started status. */
|
---|
1268 | VBGLR3GUESTCTRLCMDCTX ctx = { uClientID, VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(pSession->StartupInfo.uSessionID) };
|
---|
1269 | int rc2 = VbglR3GuestCtrlSessionNotify(&ctx, GUEST_SESSION_NOTIFYTYPE_STARTED, VINF_SUCCESS);
|
---|
1270 | if (RT_FAILURE(rc2))
|
---|
1271 | {
|
---|
1272 | VGSvcError("Reporting session ID=%RU32 started status failed with rc=%Rrc\n", pSession->StartupInfo.uSessionID, rc2);
|
---|
1273 |
|
---|
1274 | /*
|
---|
1275 | * If session status cannot be posted to the host for
|
---|
1276 | * some reason, bail out.
|
---|
1277 | */
|
---|
1278 | if (RT_SUCCESS(rc))
|
---|
1279 | rc = rc2;
|
---|
1280 | }
|
---|
1281 |
|
---|
1282 | /* Allocate a scratch buffer for commands which also send
|
---|
1283 | * payload data with them. */
|
---|
1284 | uint32_t cbScratchBuf = _64K; /** @todo Make buffer size configurable via guest properties/argv! */
|
---|
1285 | AssertReturn(RT_IS_POWER_OF_TWO(cbScratchBuf), RTEXITCODE_FAILURE);
|
---|
1286 | uint8_t *pvScratchBuf = NULL;
|
---|
1287 |
|
---|
1288 | if (RT_SUCCESS(rc))
|
---|
1289 | {
|
---|
1290 | pvScratchBuf = (uint8_t*)RTMemAlloc(cbScratchBuf);
|
---|
1291 | if (!pvScratchBuf)
|
---|
1292 | rc = VERR_NO_MEMORY;
|
---|
1293 | }
|
---|
1294 |
|
---|
1295 | if (RT_SUCCESS(rc))
|
---|
1296 | {
|
---|
1297 | bool fShutdown = false;
|
---|
1298 |
|
---|
1299 | VBGLR3GUESTCTRLCMDCTX ctxHost = { uClientID, 0 /* Context ID */, pSession->StartupInfo.uProtocol };
|
---|
1300 | for (;;)
|
---|
1301 | {
|
---|
1302 | VGSvcVerbose(3, "Waiting for host msg ...\n");
|
---|
1303 | uint32_t uMsg = 0;
|
---|
1304 | uint32_t cParms = 0;
|
---|
1305 | rc = VbglR3GuestCtrlMsgWaitFor(uClientID, &uMsg, &cParms);
|
---|
1306 | if (rc == VERR_TOO_MUCH_DATA)
|
---|
1307 | {
|
---|
1308 | #ifdef DEBUG
|
---|
1309 | VGSvcVerbose(4, "Message requires %RU32 parameters, but only 2 supplied -- retrying request (no error!)...\n",
|
---|
1310 | cParms);
|
---|
1311 | #endif
|
---|
1312 | rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
|
---|
1313 | }
|
---|
1314 | else if (RT_FAILURE(rc))
|
---|
1315 | VGSvcVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
|
---|
1316 | if (RT_SUCCESS(rc))
|
---|
1317 | {
|
---|
1318 | VGSvcVerbose(4, "Msg=%RU32 (%RU32 parms) retrieved\n", uMsg, cParms);
|
---|
1319 |
|
---|
1320 | /* Set number of parameters for current host context. */
|
---|
1321 | ctxHost.uNumParms = cParms;
|
---|
1322 |
|
---|
1323 | /* ... and pass it on to the session handler. */
|
---|
1324 | rc = VGSvcGstCtrlSessionHandler(pSession, uMsg, &ctxHost, pvScratchBuf, cbScratchBuf, &fShutdown);
|
---|
1325 | }
|
---|
1326 |
|
---|
1327 | if (fShutdown)
|
---|
1328 | break;
|
---|
1329 |
|
---|
1330 | /* Let others run ... */
|
---|
1331 | RTThreadYield();
|
---|
1332 | }
|
---|
1333 | }
|
---|
1334 |
|
---|
1335 | VGSvcVerbose(0, "Session %RU32 ended\n", pSession->StartupInfo.uSessionID);
|
---|
1336 |
|
---|
1337 | if (pvScratchBuf)
|
---|
1338 | RTMemFree(pvScratchBuf);
|
---|
1339 |
|
---|
1340 | if (uClientID)
|
---|
1341 | {
|
---|
1342 | VGSvcVerbose(3, "Disconnecting client ID=%RU32 ...\n", uClientID);
|
---|
1343 | VbglR3GuestCtrlDisconnect(uClientID);
|
---|
1344 | }
|
---|
1345 |
|
---|
1346 | VGSvcVerbose(3, "Session worker returned with rc=%Rrc\n", rc);
|
---|
1347 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1348 | }
|
---|
1349 |
|
---|
1350 |
|
---|
1351 | /**
|
---|
1352 | * Finds a (formerly) started guest process given by its PID and increases its
|
---|
1353 | * reference count.
|
---|
1354 | *
|
---|
1355 | * Must be decreased by the caller with VGSvcGstCtrlProcessRelease().
|
---|
1356 | *
|
---|
1357 | * @returns Guest process if found, otherwise NULL.
|
---|
1358 | * @param pSession Pointer to guest session where to search process in.
|
---|
1359 | * @param uPID PID to search for.
|
---|
1360 | *
|
---|
1361 | * @note This does *not lock the process!
|
---|
1362 | */
|
---|
1363 | PVBOXSERVICECTRLPROCESS VGSvcGstCtrlSessionRetainProcess(PVBOXSERVICECTRLSESSION pSession, uint32_t uPID)
|
---|
1364 | {
|
---|
1365 | AssertPtrReturn(pSession, NULL);
|
---|
1366 |
|
---|
1367 | PVBOXSERVICECTRLPROCESS pProcess = NULL;
|
---|
1368 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1369 | if (RT_SUCCESS(rc))
|
---|
1370 | {
|
---|
1371 | PVBOXSERVICECTRLPROCESS pCurProcess;
|
---|
1372 | RTListForEach(&pSession->lstProcesses, pCurProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
1373 | {
|
---|
1374 | if (pCurProcess->uPID == uPID)
|
---|
1375 | {
|
---|
1376 | rc = RTCritSectEnter(&pCurProcess->CritSect);
|
---|
1377 | if (RT_SUCCESS(rc))
|
---|
1378 | {
|
---|
1379 | pCurProcess->cRefs++;
|
---|
1380 | rc = RTCritSectLeave(&pCurProcess->CritSect);
|
---|
1381 | AssertRC(rc);
|
---|
1382 | }
|
---|
1383 |
|
---|
1384 | if (RT_SUCCESS(rc))
|
---|
1385 | pProcess = pCurProcess;
|
---|
1386 | break;
|
---|
1387 | }
|
---|
1388 | }
|
---|
1389 |
|
---|
1390 | rc = RTCritSectLeave(&pSession->CritSect);
|
---|
1391 | AssertRC(rc);
|
---|
1392 | }
|
---|
1393 |
|
---|
1394 | return pProcess;
|
---|
1395 | }
|
---|
1396 |
|
---|
1397 |
|
---|
1398 | int VGSvcGstCtrlSessionClose(PVBOXSERVICECTRLSESSION pSession)
|
---|
1399 | {
|
---|
1400 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1401 |
|
---|
1402 | VGSvcVerbose(0, "Session %RU32 is about to close ...\n", pSession->StartupInfo.uSessionID);
|
---|
1403 |
|
---|
1404 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1405 | if (RT_SUCCESS(rc))
|
---|
1406 | {
|
---|
1407 | /*
|
---|
1408 | * Close all guest processes.
|
---|
1409 | */
|
---|
1410 | VGSvcVerbose(0, "Stopping all guest processes ...\n");
|
---|
1411 |
|
---|
1412 | /* Signal all guest processes in the active list that we want to shutdown. */
|
---|
1413 | size_t cProcesses = 0;
|
---|
1414 | PVBOXSERVICECTRLPROCESS pProcess;
|
---|
1415 | RTListForEach(&pSession->lstProcesses, pProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
1416 | {
|
---|
1417 | VGSvcGstCtrlProcessStop(pProcess);
|
---|
1418 | cProcesses++;
|
---|
1419 | }
|
---|
1420 |
|
---|
1421 | VGSvcVerbose(1, "%zu guest processes were signalled to stop\n", cProcesses);
|
---|
1422 |
|
---|
1423 | /* Wait for all active threads to shutdown and destroy the active thread list. */
|
---|
1424 | pProcess = RTListGetFirst(&pSession->lstProcesses, VBOXSERVICECTRLPROCESS, Node);
|
---|
1425 | while (pProcess)
|
---|
1426 | {
|
---|
1427 | PVBOXSERVICECTRLPROCESS pNext = RTListNodeGetNext(&pProcess->Node, VBOXSERVICECTRLPROCESS, Node);
|
---|
1428 | bool fLast = RTListNodeIsLast(&pSession->lstProcesses, &pProcess->Node);
|
---|
1429 |
|
---|
1430 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
1431 | AssertRC(rc2);
|
---|
1432 |
|
---|
1433 | rc2 = VGSvcGstCtrlProcessWait(pProcess, 30 * 1000 /* Wait 30 seconds max. */, NULL /* rc */);
|
---|
1434 |
|
---|
1435 | int rc3 = RTCritSectEnter(&pSession->CritSect);
|
---|
1436 | AssertRC(rc3);
|
---|
1437 |
|
---|
1438 | if (RT_SUCCESS(rc2))
|
---|
1439 | VGSvcGstCtrlProcessFree(pProcess);
|
---|
1440 |
|
---|
1441 | if (fLast)
|
---|
1442 | break;
|
---|
1443 |
|
---|
1444 | pProcess = pNext;
|
---|
1445 | }
|
---|
1446 |
|
---|
1447 | #ifdef DEBUG
|
---|
1448 | pProcess = RTListGetFirst(&pSession->lstProcesses, VBOXSERVICECTRLPROCESS, Node);
|
---|
1449 | while (pProcess)
|
---|
1450 | {
|
---|
1451 | PVBOXSERVICECTRLPROCESS pNext = RTListNodeGetNext(&pProcess->Node, VBOXSERVICECTRLPROCESS, Node);
|
---|
1452 | bool fLast = RTListNodeIsLast(&pSession->lstProcesses, &pProcess->Node);
|
---|
1453 |
|
---|
1454 | VGSvcVerbose(1, "Process %p (PID %RU32) still in list\n", pProcess, pProcess->uPID);
|
---|
1455 | if (fLast)
|
---|
1456 | break;
|
---|
1457 |
|
---|
1458 | pProcess = pNext;
|
---|
1459 | }
|
---|
1460 | #endif
|
---|
1461 | AssertMsg(RTListIsEmpty(&pSession->lstProcesses),
|
---|
1462 | ("Guest process list still contains entries when it should not\n"));
|
---|
1463 |
|
---|
1464 | /*
|
---|
1465 | * Close all left guest files.
|
---|
1466 | */
|
---|
1467 | VGSvcVerbose(0, "Closing all guest files ...\n");
|
---|
1468 |
|
---|
1469 | PVBOXSERVICECTRLFILE pFile;
|
---|
1470 | pFile = RTListGetFirst(&pSession->lstFiles, VBOXSERVICECTRLFILE, Node);
|
---|
1471 | while (pFile)
|
---|
1472 | {
|
---|
1473 | PVBOXSERVICECTRLFILE pNext = RTListNodeGetNext(&pFile->Node, VBOXSERVICECTRLFILE, Node);
|
---|
1474 | bool fLast = RTListNodeIsLast(&pSession->lstFiles, &pFile->Node);
|
---|
1475 |
|
---|
1476 | int rc2 = vgsvcGstCtrlSessionFileDestroy(pFile);
|
---|
1477 | if (RT_FAILURE(rc2))
|
---|
1478 | {
|
---|
1479 | VGSvcError("Unable to close file '%s'; rc=%Rrc\n", pFile->szName, rc2);
|
---|
1480 | if (RT_SUCCESS(rc))
|
---|
1481 | rc = rc2;
|
---|
1482 | /* Keep going. */
|
---|
1483 | }
|
---|
1484 |
|
---|
1485 | if (fLast)
|
---|
1486 | break;
|
---|
1487 |
|
---|
1488 | pFile = pNext;
|
---|
1489 | }
|
---|
1490 |
|
---|
1491 | AssertMsg(RTListIsEmpty(&pSession->lstFiles), ("Guest file list still contains entries when it should not\n"));
|
---|
1492 |
|
---|
1493 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
1494 | if (RT_SUCCESS(rc))
|
---|
1495 | rc = rc2;
|
---|
1496 | }
|
---|
1497 |
|
---|
1498 | return rc;
|
---|
1499 | }
|
---|
1500 |
|
---|
1501 |
|
---|
1502 | int VGSvcGstCtrlSessionDestroy(PVBOXSERVICECTRLSESSION pSession)
|
---|
1503 | {
|
---|
1504 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1505 |
|
---|
1506 | int rc = VGSvcGstCtrlSessionClose(pSession);
|
---|
1507 |
|
---|
1508 | /* Destroy critical section. */
|
---|
1509 | RTCritSectDelete(&pSession->CritSect);
|
---|
1510 |
|
---|
1511 | return rc;
|
---|
1512 | }
|
---|
1513 |
|
---|
1514 |
|
---|
1515 | int VGSvcGstCtrlSessionInit(PVBOXSERVICECTRLSESSION pSession, uint32_t fFlags)
|
---|
1516 | {
|
---|
1517 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1518 |
|
---|
1519 | RTListInit(&pSession->lstProcesses);
|
---|
1520 | RTListInit(&pSession->lstFiles);
|
---|
1521 |
|
---|
1522 | pSession->fFlags = fFlags;
|
---|
1523 |
|
---|
1524 | /* Init critical section for protecting the thread lists. */
|
---|
1525 | int rc = RTCritSectInit(&pSession->CritSect);
|
---|
1526 | AssertRC(rc);
|
---|
1527 |
|
---|
1528 | return rc;
|
---|
1529 | }
|
---|
1530 |
|
---|
1531 |
|
---|
1532 | /**
|
---|
1533 | * Adds a guest process to a session's process list.
|
---|
1534 | *
|
---|
1535 | * @return VBox status code.
|
---|
1536 | * @param pSession Guest session to add process to.
|
---|
1537 | * @param pProcess Guest process to add.
|
---|
1538 | */
|
---|
1539 | int VGSvcGstCtrlSessionProcessAdd(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess)
|
---|
1540 | {
|
---|
1541 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1542 | AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
|
---|
1543 |
|
---|
1544 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1545 | if (RT_SUCCESS(rc))
|
---|
1546 | {
|
---|
1547 | VGSvcVerbose( 3, "Adding process (PID %RU32) to session ID=%RU32\n", pProcess->uPID, pSession->StartupInfo.uSessionID);
|
---|
1548 |
|
---|
1549 | /* Add process to session list. */
|
---|
1550 | RTListAppend(&pSession->lstProcesses, &pProcess->Node);
|
---|
1551 |
|
---|
1552 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
1553 | if (RT_SUCCESS(rc))
|
---|
1554 | rc = rc2;
|
---|
1555 | }
|
---|
1556 |
|
---|
1557 | return VINF_SUCCESS;
|
---|
1558 | }
|
---|
1559 |
|
---|
1560 |
|
---|
1561 | /**
|
---|
1562 | * Removes a guest process from a session's process list.
|
---|
1563 | *
|
---|
1564 | * @return VBox status code.
|
---|
1565 | * @param pSession Guest session to remove process from.
|
---|
1566 | * @param pProcess Guest process to remove.
|
---|
1567 | */
|
---|
1568 | int VGSvcGstCtrlSessionProcessRemove(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess)
|
---|
1569 | {
|
---|
1570 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1571 | AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
|
---|
1572 |
|
---|
1573 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1574 | if (RT_SUCCESS(rc))
|
---|
1575 | {
|
---|
1576 | VGSvcVerbose(3, "Removing process (PID %RU32) from session ID=%RU32\n", pProcess->uPID, pSession->StartupInfo.uSessionID);
|
---|
1577 | Assert(pProcess->cRefs == 0);
|
---|
1578 |
|
---|
1579 | RTListNodeRemove(&pProcess->Node);
|
---|
1580 |
|
---|
1581 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
1582 | if (RT_SUCCESS(rc))
|
---|
1583 | rc = rc2;
|
---|
1584 | }
|
---|
1585 |
|
---|
1586 | return VINF_SUCCESS;
|
---|
1587 | }
|
---|
1588 |
|
---|
1589 |
|
---|
1590 | /**
|
---|
1591 | * Determines whether starting a new guest process according to the
|
---|
1592 | * maximum number of concurrent guest processes defined is allowed or not.
|
---|
1593 | *
|
---|
1594 | * @return VBox status code.
|
---|
1595 | * @param pSession The guest session.
|
---|
1596 | * @param pbAllowed True if starting (another) guest process
|
---|
1597 | * is allowed, false if not.
|
---|
1598 | */
|
---|
1599 | int VGSvcGstCtrlSessionProcessStartAllowed(const PVBOXSERVICECTRLSESSION pSession, bool *pbAllowed)
|
---|
1600 | {
|
---|
1601 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1602 | AssertPtrReturn(pbAllowed, VERR_INVALID_POINTER);
|
---|
1603 |
|
---|
1604 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1605 | if (RT_SUCCESS(rc))
|
---|
1606 | {
|
---|
1607 | /*
|
---|
1608 | * Check if we're respecting our memory policy by checking
|
---|
1609 | * how many guest processes are started and served already.
|
---|
1610 | */
|
---|
1611 | bool fLimitReached = false;
|
---|
1612 | if (pSession->uProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
|
---|
1613 | {
|
---|
1614 | uint32_t uProcsRunning = 0;
|
---|
1615 | PVBOXSERVICECTRLPROCESS pProcess;
|
---|
1616 | RTListForEach(&pSession->lstProcesses, pProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
1617 | uProcsRunning++;
|
---|
1618 |
|
---|
1619 | VGSvcVerbose(3, "Maximum served guest processes set to %u, running=%u\n", pSession->uProcsMaxKept, uProcsRunning);
|
---|
1620 |
|
---|
1621 | int32_t iProcsLeft = (pSession->uProcsMaxKept - uProcsRunning - 1);
|
---|
1622 | if (iProcsLeft < 0)
|
---|
1623 | {
|
---|
1624 | VGSvcVerbose(3, "Maximum running guest processes reached (%u)\n", pSession->uProcsMaxKept);
|
---|
1625 | fLimitReached = true;
|
---|
1626 | }
|
---|
1627 | }
|
---|
1628 |
|
---|
1629 | *pbAllowed = !fLimitReached;
|
---|
1630 |
|
---|
1631 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
1632 | if (RT_SUCCESS(rc))
|
---|
1633 | rc = rc2;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | return rc;
|
---|
1637 | }
|
---|
1638 |
|
---|
1639 |
|
---|
1640 | /**
|
---|
1641 | * Creates the process for a guest session.
|
---|
1642 | *
|
---|
1643 | *
|
---|
1644 | * @return VBox status code.
|
---|
1645 | * @param pSessionStartupInfo Session startup info.
|
---|
1646 | * @param pSessionThread The session thread under construction.
|
---|
1647 | * @param uCtrlSessionThread The session thread debug ordinal.
|
---|
1648 | */
|
---|
1649 | static int vgsvcVGSvcGstCtrlSessionThreadCreateProcess(const PVBOXSERVICECTRLSESSIONSTARTUPINFO pSessionStartupInfo,
|
---|
1650 | PVBOXSERVICECTRLSESSIONTHREAD pSessionThread, uint32_t uCtrlSessionThread)
|
---|
1651 | {
|
---|
1652 | RT_NOREF1(uCtrlSessionThread);
|
---|
1653 |
|
---|
1654 | /*
|
---|
1655 | * Is this an anonymous session? Anonymous sessions run with the same
|
---|
1656 | * privileges as the main VBoxService executable.
|
---|
1657 | */
|
---|
1658 | bool const fAnonymous = pSessionThread->StartupInfo.szUser[0] == '\0';
|
---|
1659 | if (fAnonymous)
|
---|
1660 | {
|
---|
1661 | Assert(!strlen(pSessionThread->StartupInfo.szPassword));
|
---|
1662 | Assert(!strlen(pSessionThread->StartupInfo.szDomain));
|
---|
1663 |
|
---|
1664 | VGSvcVerbose(3, "New anonymous guest session ID=%RU32 created, fFlags=%x, using protocol %RU32\n",
|
---|
1665 | pSessionStartupInfo->uSessionID,
|
---|
1666 | pSessionStartupInfo->fFlags,
|
---|
1667 | pSessionStartupInfo->uProtocol);
|
---|
1668 | }
|
---|
1669 | else
|
---|
1670 | {
|
---|
1671 | VGSvcVerbose(3, "Spawning new guest session ID=%RU32, szUser=%s, szPassword=%s, szDomain=%s, fFlags=%x, using protocol %RU32\n",
|
---|
1672 | pSessionStartupInfo->uSessionID,
|
---|
1673 | pSessionStartupInfo->szUser,
|
---|
1674 | #ifdef DEBUG
|
---|
1675 | pSessionStartupInfo->szPassword,
|
---|
1676 | #else
|
---|
1677 | "XXX", /* Never show passwords in release mode. */
|
---|
1678 | #endif
|
---|
1679 | pSessionStartupInfo->szDomain,
|
---|
1680 | pSessionStartupInfo->fFlags,
|
---|
1681 | pSessionStartupInfo->uProtocol);
|
---|
1682 | }
|
---|
1683 |
|
---|
1684 | /*
|
---|
1685 | * Spawn a child process for doing the actual session handling.
|
---|
1686 | * Start by assembling the argument list.
|
---|
1687 | */
|
---|
1688 | int rc = VINF_SUCCESS;
|
---|
1689 | char szExeName[RTPATH_MAX];
|
---|
1690 | char *pszExeName = RTProcGetExecutablePath(szExeName, sizeof(szExeName));
|
---|
1691 | if (pszExeName)
|
---|
1692 | {
|
---|
1693 | char szParmSessionID[32];
|
---|
1694 | RTStrPrintf(szParmSessionID, sizeof(szParmSessionID), "--session-id=%RU32", pSessionThread->StartupInfo.uSessionID);
|
---|
1695 |
|
---|
1696 | char szParmSessionProto[32];
|
---|
1697 | RTStrPrintf(szParmSessionProto, sizeof(szParmSessionProto), "--session-proto=%RU32",
|
---|
1698 | pSessionThread->StartupInfo.uProtocol);
|
---|
1699 | #ifdef DEBUG
|
---|
1700 | char szParmThreadId[32];
|
---|
1701 | RTStrPrintf(szParmThreadId, sizeof(szParmThreadId), "--thread-id=%RU32", uCtrlSessionThread);
|
---|
1702 | #endif
|
---|
1703 | unsigned idxArg = 0; /* Next index in argument vector. */
|
---|
1704 | char const *apszArgs[24];
|
---|
1705 |
|
---|
1706 | apszArgs[idxArg++] = pszExeName;
|
---|
1707 | apszArgs[idxArg++] = "guestsession";
|
---|
1708 | apszArgs[idxArg++] = szParmSessionID;
|
---|
1709 | apszArgs[idxArg++] = szParmSessionProto;
|
---|
1710 | #ifdef DEBUG
|
---|
1711 | apszArgs[idxArg++] = szParmThreadId;
|
---|
1712 | #endif
|
---|
1713 | if (!fAnonymous) /* Do we need to pass a user name? */
|
---|
1714 | {
|
---|
1715 | apszArgs[idxArg++] = "--user";
|
---|
1716 | apszArgs[idxArg++] = pSessionThread->StartupInfo.szUser;
|
---|
1717 |
|
---|
1718 | if (strlen(pSessionThread->StartupInfo.szDomain))
|
---|
1719 | {
|
---|
1720 | apszArgs[idxArg++] = "--domain";
|
---|
1721 | apszArgs[idxArg++] = pSessionThread->StartupInfo.szDomain;
|
---|
1722 | }
|
---|
1723 | }
|
---|
1724 |
|
---|
1725 | /* Add same verbose flags as parent process. */
|
---|
1726 | char szParmVerbose[32];
|
---|
1727 | if (g_cVerbosity > 0)
|
---|
1728 | {
|
---|
1729 | unsigned cVs = RT_MIN(g_cVerbosity, RT_ELEMENTS(szParmVerbose) - 2);
|
---|
1730 | szParmVerbose[0] = '-';
|
---|
1731 | memset(&szParmVerbose[1], 'v', cVs);
|
---|
1732 | szParmVerbose[1 + cVs] = '\0';
|
---|
1733 | apszArgs[idxArg++] = szParmVerbose;
|
---|
1734 | }
|
---|
1735 |
|
---|
1736 | /* Add log file handling. Each session will have an own
|
---|
1737 | * log file, naming based on the parent log file. */
|
---|
1738 | char szParmLogFile[sizeof(g_szLogFile) + 128];
|
---|
1739 | if (g_szLogFile[0])
|
---|
1740 | {
|
---|
1741 | const char *pszSuffix = RTPathSuffix(g_szLogFile);
|
---|
1742 | if (!pszSuffix)
|
---|
1743 | pszSuffix = strchr(g_szLogFile, '\0');
|
---|
1744 | size_t cchBase = pszSuffix - g_szLogFile;
|
---|
1745 | #ifndef DEBUG
|
---|
1746 | RTStrPrintf(szParmLogFile, sizeof(szParmLogFile), "%.*s-%RU32-%s%s",
|
---|
1747 | cchBase, g_szLogFile, pSessionStartupInfo->uSessionID, pSessionStartupInfo->szUser, pszSuffix);
|
---|
1748 | #else
|
---|
1749 | RTStrPrintf(szParmLogFile, sizeof(szParmLogFile), "%.*s-%RU32-%RU32-%s%s",
|
---|
1750 | cchBase, g_szLogFile, pSessionStartupInfo->uSessionID, uCtrlSessionThread,
|
---|
1751 | pSessionStartupInfo->szUser, pszSuffix);
|
---|
1752 | #endif
|
---|
1753 | apszArgs[idxArg++] = "--logfile";
|
---|
1754 | apszArgs[idxArg++] = szParmLogFile;
|
---|
1755 | }
|
---|
1756 |
|
---|
1757 | #ifdef DEBUG
|
---|
1758 | VGSvcVerbose(4, "Argv building rc=%Rrc, session flags=%x\n", rc, g_Session.fFlags);
|
---|
1759 | if (RT_SUCCESS(rc))
|
---|
1760 | {
|
---|
1761 | if (g_Session.fFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT)
|
---|
1762 | apszArgs[idxArg++] = "--dump-stdout";
|
---|
1763 | if (g_Session.fFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR)
|
---|
1764 | apszArgs[idxArg++] = "--dump-stderr";
|
---|
1765 | }
|
---|
1766 | #endif
|
---|
1767 | apszArgs[idxArg] = NULL;
|
---|
1768 | Assert(idxArg < RT_ELEMENTS(apszArgs));
|
---|
1769 |
|
---|
1770 | if (g_cVerbosity > 3)
|
---|
1771 | {
|
---|
1772 | VGSvcVerbose(4, "Spawning parameters:\n");
|
---|
1773 | for (idxArg = 0; apszArgs[idxArg]; idxArg++)
|
---|
1774 | VGSvcVerbose(4, "\t%s\n", apszArgs[idxArg]);
|
---|
1775 | }
|
---|
1776 |
|
---|
1777 | /*
|
---|
1778 | * Configure standard handles and finally create the process.
|
---|
1779 | */
|
---|
1780 | uint32_t fProcCreate = RTPROC_FLAGS_PROFILE;
|
---|
1781 | #ifdef RT_OS_WINDOWS /* Windows only flags: */
|
---|
1782 | fProcCreate |= RTPROC_FLAGS_SERVICE
|
---|
1783 | | RTPROC_FLAGS_HIDDEN; /** @todo More flags from startup info? */
|
---|
1784 | #endif
|
---|
1785 |
|
---|
1786 | #if 0 /* Pipe handling not needed (yet). */
|
---|
1787 | /* Setup pipes. */
|
---|
1788 | rc = GstcntlProcessSetupPipe("|", 0 /*STDIN_FILENO*/,
|
---|
1789 | &pSession->StdIn.hChild, &pSession->StdIn.phChild, &pSession->hStdInW);
|
---|
1790 | if (RT_SUCCESS(rc))
|
---|
1791 | {
|
---|
1792 | rc = GstcntlProcessSetupPipe("|", 1 /*STDOUT_FILENO*/,
|
---|
1793 | &pSession->StdOut.hChild, &pSession->StdOut.phChild, &pSession->hStdOutR);
|
---|
1794 | if (RT_SUCCESS(rc))
|
---|
1795 | {
|
---|
1796 | rc = GstcntlProcessSetupPipe("|", 2 /*STDERR_FILENO*/,
|
---|
1797 | &pSession->StdErr.hChild, &pSession->StdErr.phChild, &pSession->hStdErrR);
|
---|
1798 | if (RT_SUCCESS(rc))
|
---|
1799 | {
|
---|
1800 | rc = RTPollSetCreate(&pSession->hPollSet);
|
---|
1801 | if (RT_SUCCESS(rc))
|
---|
1802 | rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdInW, RTPOLL_EVT_ERROR,
|
---|
1803 | VBOXSERVICECTRLPIPEID_STDIN);
|
---|
1804 | if (RT_SUCCESS(rc))
|
---|
1805 | rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
1806 | VBOXSERVICECTRLPIPEID_STDOUT);
|
---|
1807 | if (RT_SUCCESS(rc))
|
---|
1808 | rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
|
---|
1809 | VBOXSERVICECTRLPIPEID_STDERR);
|
---|
1810 | }
|
---|
1811 |
|
---|
1812 | if (RT_SUCCESS(rc))
|
---|
1813 | rc = RTProcCreateEx(pszExeName, apszArgs, hEnv, fProcCreate,
|
---|
1814 | pSession->StdIn.phChild, pSession->StdOut.phChild, pSession->StdErr.phChild,
|
---|
1815 | !fAnonymous ? pSession->StartupInfo.szUser : NULL,
|
---|
1816 | !fAnonymous ? pSession->StartupInfo.szPassword : NULL,
|
---|
1817 | &pSession->hProcess);
|
---|
1818 |
|
---|
1819 | if (RT_SUCCESS(rc))
|
---|
1820 | {
|
---|
1821 | /*
|
---|
1822 | * Close the child ends of any pipes and redirected files.
|
---|
1823 | */
|
---|
1824 | int rc2 = RTHandleClose(pSession->StdIn.phChild); AssertRC(rc2);
|
---|
1825 | pSession->StdIn.phChild = NULL;
|
---|
1826 | rc2 = RTHandleClose(pSession->StdOut.phChild); AssertRC(rc2);
|
---|
1827 | pSession->StdOut.phChild = NULL;
|
---|
1828 | rc2 = RTHandleClose(pSession->StdErr.phChild); AssertRC(rc2);
|
---|
1829 | pSession->StdErr.phChild = NULL;
|
---|
1830 | }
|
---|
1831 | }
|
---|
1832 | }
|
---|
1833 | #else
|
---|
1834 | if (RT_SUCCESS(rc))
|
---|
1835 | {
|
---|
1836 | RTHANDLE hStdIn;
|
---|
1837 | rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
|
---|
1838 | if (RT_SUCCESS(rc))
|
---|
1839 | {
|
---|
1840 | hStdIn.enmType = RTHANDLETYPE_FILE;
|
---|
1841 |
|
---|
1842 | RTHANDLE hStdOutAndErr;
|
---|
1843 | rc = RTFileOpenBitBucket(&hStdOutAndErr.u.hFile, RTFILE_O_WRITE);
|
---|
1844 | if (RT_SUCCESS(rc))
|
---|
1845 | {
|
---|
1846 | hStdOutAndErr.enmType = RTHANDLETYPE_FILE;
|
---|
1847 |
|
---|
1848 | const char *pszUser = pSessionThread->StartupInfo.szUser;
|
---|
1849 | # ifdef RT_OS_WINDOWS
|
---|
1850 | /* If a domain name is given, construct an UPN (User Principle Name) with
|
---|
1851 | * the domain name built-in, e.g. "[email protected]". */
|
---|
1852 | char *pszUserUPN = NULL;
|
---|
1853 | if (strlen(pSessionThread->StartupInfo.szDomain))
|
---|
1854 | {
|
---|
1855 | int cbUserUPN = RTStrAPrintf(&pszUserUPN, "%s@%s",
|
---|
1856 | pSessionThread->StartupInfo.szUser,
|
---|
1857 | pSessionThread->StartupInfo.szDomain);
|
---|
1858 | if (cbUserUPN > 0)
|
---|
1859 | {
|
---|
1860 | pszUser = pszUserUPN;
|
---|
1861 | VGSvcVerbose(3, "Using UPN: %s\n", pszUserUPN);
|
---|
1862 | }
|
---|
1863 | }
|
---|
1864 | # endif
|
---|
1865 |
|
---|
1866 | rc = RTProcCreateEx(pszExeName, apszArgs, RTENV_DEFAULT, fProcCreate,
|
---|
1867 | &hStdIn, &hStdOutAndErr, &hStdOutAndErr,
|
---|
1868 | !fAnonymous ? pszUser : NULL,
|
---|
1869 | !fAnonymous ? pSessionThread->StartupInfo.szPassword : NULL,
|
---|
1870 | &pSessionThread->hProcess);
|
---|
1871 | # ifdef RT_OS_WINDOWS
|
---|
1872 | if (pszUserUPN)
|
---|
1873 | RTStrFree(pszUserUPN);
|
---|
1874 | # endif
|
---|
1875 | RTFileClose(hStdOutAndErr.u.hFile);
|
---|
1876 | }
|
---|
1877 |
|
---|
1878 | RTFileClose(hStdIn.u.hFile);
|
---|
1879 | }
|
---|
1880 | }
|
---|
1881 | #endif
|
---|
1882 | }
|
---|
1883 | else
|
---|
1884 | rc = VERR_FILE_NOT_FOUND;
|
---|
1885 | return rc;
|
---|
1886 | }
|
---|
1887 |
|
---|
1888 |
|
---|
1889 | /**
|
---|
1890 | * Creates a guest session.
|
---|
1891 | *
|
---|
1892 | * This will spawn a new VBoxService.exe instance under behalf of the given user
|
---|
1893 | * which then will act as a session host. On successful open, the session will
|
---|
1894 | * be added to the given session thread list.
|
---|
1895 | *
|
---|
1896 | * @return VBox status code.
|
---|
1897 | * @param pList Which list to use to store the session thread in.
|
---|
1898 | * @param pSessionStartupInfo Session startup info.
|
---|
1899 | * @param ppSessionThread Returns newly created session thread on success.
|
---|
1900 | * Optional.
|
---|
1901 | */
|
---|
1902 | int VGSvcGstCtrlSessionThreadCreate(PRTLISTANCHOR pList, const PVBOXSERVICECTRLSESSIONSTARTUPINFO pSessionStartupInfo,
|
---|
1903 | PVBOXSERVICECTRLSESSIONTHREAD *ppSessionThread)
|
---|
1904 | {
|
---|
1905 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
1906 | AssertPtrReturn(pSessionStartupInfo, VERR_INVALID_POINTER);
|
---|
1907 | /* ppSessionThread is optional. */
|
---|
1908 |
|
---|
1909 | #ifdef VBOX_STRICT
|
---|
1910 | /* Check for existing session in debug mode. Should never happen because of
|
---|
1911 | * Main consistency. */
|
---|
1912 | PVBOXSERVICECTRLSESSIONTHREAD pSessionCur;
|
---|
1913 | RTListForEach(pList, pSessionCur, VBOXSERVICECTRLSESSIONTHREAD, Node)
|
---|
1914 | {
|
---|
1915 | AssertMsgReturn(pSessionCur->StartupInfo.uSessionID != pSessionStartupInfo->uSessionID,
|
---|
1916 | ("Guest session thread ID=%RU32 (%p) already exists when it should not\n",
|
---|
1917 | pSessionCur->StartupInfo.uSessionID, pSessionCur), VERR_ALREADY_EXISTS);
|
---|
1918 | }
|
---|
1919 | #endif
|
---|
1920 | int rc = VINF_SUCCESS;
|
---|
1921 |
|
---|
1922 | /* Static counter to help tracking session thread <-> process relations. */
|
---|
1923 | static uint32_t s_uCtrlSessionThread = 0;
|
---|
1924 | if (s_uCtrlSessionThread++ == UINT32_MAX)
|
---|
1925 | s_uCtrlSessionThread = 0; /* Wrap around to not let IPRT freak out. */
|
---|
1926 |
|
---|
1927 | /*
|
---|
1928 | * Allocate and initialize the session thread structure.
|
---|
1929 | */
|
---|
1930 | PVBOXSERVICECTRLSESSIONTHREAD pSessionThread = (PVBOXSERVICECTRLSESSIONTHREAD)RTMemAllocZ(sizeof(*pSessionThread));
|
---|
1931 | if (pSessionThread)
|
---|
1932 | {
|
---|
1933 | /* Copy over session startup info. */
|
---|
1934 | memcpy(&pSessionThread->StartupInfo, pSessionStartupInfo, sizeof(VBOXSERVICECTRLSESSIONSTARTUPINFO));
|
---|
1935 |
|
---|
1936 | pSessionThread->fShutdown = false;
|
---|
1937 | pSessionThread->fStarted = false;
|
---|
1938 | pSessionThread->fStopped = false;
|
---|
1939 |
|
---|
1940 | rc = RTCritSectInit(&pSessionThread->CritSect);
|
---|
1941 | AssertRC(rc);
|
---|
1942 | if (RT_SUCCESS(rc))
|
---|
1943 | {
|
---|
1944 | /*
|
---|
1945 | * Start the session thread.
|
---|
1946 | */
|
---|
1947 | rc = vgsvcVGSvcGstCtrlSessionThreadCreateProcess(pSessionStartupInfo, pSessionThread, s_uCtrlSessionThread);
|
---|
1948 | if (RT_SUCCESS(rc))
|
---|
1949 | {
|
---|
1950 | /*
|
---|
1951 | * Start the session thread.
|
---|
1952 | */
|
---|
1953 | rc = RTThreadCreateF(&pSessionThread->Thread, vgsvcGstCtrlSessionThread,
|
---|
1954 | pSessionThread /*pvUser*/, 0 /*cbStack*/,
|
---|
1955 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "sess%u", s_uCtrlSessionThread);
|
---|
1956 | if (RT_SUCCESS(rc))
|
---|
1957 | {
|
---|
1958 | /* Wait for the thread to initialize. */
|
---|
1959 | rc = RTThreadUserWait(pSessionThread->Thread, RT_MS_1MIN);
|
---|
1960 | if ( RT_SUCCESS(rc)
|
---|
1961 | && !ASMAtomicReadBool(&pSessionThread->fShutdown))
|
---|
1962 | {
|
---|
1963 | VGSvcVerbose(2, "Thread for session ID=%RU32 started\n", pSessionThread->StartupInfo.uSessionID);
|
---|
1964 |
|
---|
1965 | ASMAtomicXchgBool(&pSessionThread->fStarted, true);
|
---|
1966 |
|
---|
1967 | /* Add session to list. */
|
---|
1968 | RTListAppend(pList, &pSessionThread->Node);
|
---|
1969 | if (ppSessionThread) /* Return session if wanted. */
|
---|
1970 | *ppSessionThread = pSessionThread;
|
---|
1971 | return VINF_SUCCESS;
|
---|
1972 | }
|
---|
1973 |
|
---|
1974 | /*
|
---|
1975 | * Bail out.
|
---|
1976 | */
|
---|
1977 | VGSvcError("Thread for session ID=%RU32 failed to start, rc=%Rrc\n",
|
---|
1978 | pSessionThread->StartupInfo.uSessionID, rc);
|
---|
1979 | if (RT_SUCCESS_NP(rc))
|
---|
1980 | rc = VERR_CANT_CREATE; /** @todo Find a better rc. */
|
---|
1981 | }
|
---|
1982 | else
|
---|
1983 | VGSvcError("Creating session thread failed, rc=%Rrc\n", rc);
|
---|
1984 |
|
---|
1985 | RTProcTerminate(pSessionThread->hProcess);
|
---|
1986 | uint32_t cMsWait = 1;
|
---|
1987 | while ( RTProcWait(pSessionThread->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL) == VERR_PROCESS_RUNNING
|
---|
1988 | && cMsWait <= 9) /* 1023 ms */
|
---|
1989 | {
|
---|
1990 | RTThreadSleep(cMsWait);
|
---|
1991 | cMsWait <<= 1;
|
---|
1992 | }
|
---|
1993 | }
|
---|
1994 | RTCritSectDelete(&pSessionThread->CritSect);
|
---|
1995 | }
|
---|
1996 | RTMemFree(pSessionThread);
|
---|
1997 | }
|
---|
1998 | else
|
---|
1999 | rc = VERR_NO_MEMORY;
|
---|
2000 |
|
---|
2001 | VGSvcVerbose(3, "Spawning session thread returned returned rc=%Rrc\n", rc);
|
---|
2002 | return rc;
|
---|
2003 | }
|
---|
2004 |
|
---|
2005 |
|
---|
2006 | /**
|
---|
2007 | * Waits for a formerly opened guest session process to close.
|
---|
2008 | *
|
---|
2009 | * @return VBox status code.
|
---|
2010 | * @param pThread Guest session thread to wait for.
|
---|
2011 | * @param uTimeoutMS Waiting timeout (in ms).
|
---|
2012 | * @param fFlags Closing flags.
|
---|
2013 | */
|
---|
2014 | int VGSvcGstCtrlSessionThreadWait(PVBOXSERVICECTRLSESSIONTHREAD pThread, uint32_t uTimeoutMS, uint32_t fFlags)
|
---|
2015 | {
|
---|
2016 | RT_NOREF1(fFlags);
|
---|
2017 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
2018 | /** @todo Validate closing flags. */
|
---|
2019 |
|
---|
2020 | AssertMsgReturn(pThread->Thread != NIL_RTTHREAD,
|
---|
2021 | ("Guest session thread of session %p does not exist when it should\n", pThread),
|
---|
2022 | VERR_NOT_FOUND);
|
---|
2023 |
|
---|
2024 | int rc = VINF_SUCCESS;
|
---|
2025 |
|
---|
2026 | /*
|
---|
2027 | * The spawned session process should have received the same closing request,
|
---|
2028 | * so just wait for the process to close.
|
---|
2029 | */
|
---|
2030 | if (ASMAtomicReadBool(&pThread->fStarted))
|
---|
2031 | {
|
---|
2032 | /* Ask the thread to shutdown. */
|
---|
2033 | ASMAtomicXchgBool(&pThread->fShutdown, true);
|
---|
2034 |
|
---|
2035 | VGSvcVerbose(3, "Waiting for session thread ID=%RU32 to close (%RU32ms) ...\n",
|
---|
2036 | pThread->StartupInfo.uSessionID, uTimeoutMS);
|
---|
2037 |
|
---|
2038 | int rcThread;
|
---|
2039 | rc = RTThreadWait(pThread->Thread, uTimeoutMS, &rcThread);
|
---|
2040 | if (RT_SUCCESS(rc))
|
---|
2041 | VGSvcVerbose(3, "Session thread ID=%RU32 ended with rc=%Rrc\n", pThread->StartupInfo.uSessionID, rcThread);
|
---|
2042 | else
|
---|
2043 | VGSvcError("Waiting for session thread ID=%RU32 to close failed with rc=%Rrc\n", pThread->StartupInfo.uSessionID, rc);
|
---|
2044 | }
|
---|
2045 |
|
---|
2046 | return rc;
|
---|
2047 | }
|
---|
2048 |
|
---|
2049 | /**
|
---|
2050 | * Waits for the specified session thread to end and remove
|
---|
2051 | * it from the session thread list.
|
---|
2052 | *
|
---|
2053 | * @return VBox status code.
|
---|
2054 | * @param pThread Session thread to destroy.
|
---|
2055 | * @param fFlags Closing flags.
|
---|
2056 | */
|
---|
2057 | int VGSvcGstCtrlSessionThreadDestroy(PVBOXSERVICECTRLSESSIONTHREAD pThread, uint32_t fFlags)
|
---|
2058 | {
|
---|
2059 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
2060 |
|
---|
2061 | int rc = VGSvcGstCtrlSessionThreadWait(pThread, 5 * 60 * 1000 /* 5 minutes timeout */, fFlags);
|
---|
2062 |
|
---|
2063 | /* Remove session from list and destroy object. */
|
---|
2064 | RTListNodeRemove(&pThread->Node);
|
---|
2065 |
|
---|
2066 | RTMemFree(pThread);
|
---|
2067 | pThread = NULL;
|
---|
2068 |
|
---|
2069 | return rc;
|
---|
2070 | }
|
---|
2071 |
|
---|
2072 | /**
|
---|
2073 | * Close all open guest session threads.
|
---|
2074 | *
|
---|
2075 | * @note Caller is responsible for locking!
|
---|
2076 | *
|
---|
2077 | * @return VBox status code.
|
---|
2078 | * @param pList Which list to close the session threads for.
|
---|
2079 | * @param fFlags Closing flags.
|
---|
2080 | */
|
---|
2081 | int VGSvcGstCtrlSessionThreadDestroyAll(PRTLISTANCHOR pList, uint32_t fFlags)
|
---|
2082 | {
|
---|
2083 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
2084 |
|
---|
2085 | int rc = VINF_SUCCESS;
|
---|
2086 |
|
---|
2087 | /*int rc = VbglR3GuestCtrlClose
|
---|
2088 | if (RT_FAILURE(rc))
|
---|
2089 | VGSvcError("Cancelling pending waits failed; rc=%Rrc\n", rc);*/
|
---|
2090 |
|
---|
2091 | PVBOXSERVICECTRLSESSIONTHREAD pSessIt;
|
---|
2092 | PVBOXSERVICECTRLSESSIONTHREAD pSessItNext;
|
---|
2093 | RTListForEachSafe(pList, pSessIt, pSessItNext, VBOXSERVICECTRLSESSIONTHREAD, Node)
|
---|
2094 | {
|
---|
2095 | int rc2 = VGSvcGstCtrlSessionThreadDestroy(pSessIt, fFlags);
|
---|
2096 | if (RT_FAILURE(rc2))
|
---|
2097 | {
|
---|
2098 | VGSvcError("Closing session thread '%s' failed with rc=%Rrc\n", RTThreadGetName(pSessIt->Thread), rc2);
|
---|
2099 | if (RT_SUCCESS(rc))
|
---|
2100 | rc = rc2;
|
---|
2101 | /* Keep going. */
|
---|
2102 | }
|
---|
2103 | }
|
---|
2104 |
|
---|
2105 | VGSvcVerbose(4, "Destroying guest session threads ended with %Rrc\n", rc);
|
---|
2106 | return rc;
|
---|
2107 | }
|
---|
2108 |
|
---|
2109 |
|
---|
2110 | /**
|
---|
2111 | * Main function for the session process.
|
---|
2112 | *
|
---|
2113 | * @returns exit code.
|
---|
2114 | * @param argc Argument count.
|
---|
2115 | * @param argv Argument vector (UTF-8).
|
---|
2116 | */
|
---|
2117 | RTEXITCODE VGSvcGstCtrlSessionSpawnInit(int argc, char **argv)
|
---|
2118 | {
|
---|
2119 | static const RTGETOPTDEF s_aOptions[] =
|
---|
2120 | {
|
---|
2121 | { "--domain", VBOXSERVICESESSIONOPT_DOMAIN, RTGETOPT_REQ_STRING },
|
---|
2122 | #ifdef DEBUG
|
---|
2123 | { "--dump-stdout", VBOXSERVICESESSIONOPT_DUMP_STDOUT, RTGETOPT_REQ_NOTHING },
|
---|
2124 | { "--dump-stderr", VBOXSERVICESESSIONOPT_DUMP_STDERR, RTGETOPT_REQ_NOTHING },
|
---|
2125 | #endif
|
---|
2126 | { "--logfile", VBOXSERVICESESSIONOPT_LOG_FILE, RTGETOPT_REQ_STRING },
|
---|
2127 | { "--user", VBOXSERVICESESSIONOPT_USERNAME, RTGETOPT_REQ_STRING },
|
---|
2128 | { "--session-id", VBOXSERVICESESSIONOPT_SESSION_ID, RTGETOPT_REQ_UINT32 },
|
---|
2129 | { "--session-proto", VBOXSERVICESESSIONOPT_SESSION_PROTO, RTGETOPT_REQ_UINT32 },
|
---|
2130 | #ifdef DEBUG
|
---|
2131 | { "--thread-id", VBOXSERVICESESSIONOPT_THREAD_ID, RTGETOPT_REQ_UINT32 },
|
---|
2132 | #endif /* DEBUG */
|
---|
2133 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
2134 | };
|
---|
2135 |
|
---|
2136 | int ch;
|
---|
2137 | RTGETOPTUNION ValueUnion;
|
---|
2138 | RTGETOPTSTATE GetState;
|
---|
2139 | RTGetOptInit(&GetState, argc, argv,
|
---|
2140 | s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
2141 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
2142 |
|
---|
2143 | uint32_t fSession = VBOXSERVICECTRLSESSION_FLAG_SPAWN;
|
---|
2144 |
|
---|
2145 | /* Protocol and session ID must be specified explicitly. */
|
---|
2146 | g_Session.StartupInfo.uProtocol = UINT32_MAX;
|
---|
2147 | g_Session.StartupInfo.uSessionID = UINT32_MAX;
|
---|
2148 |
|
---|
2149 | while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
2150 | {
|
---|
2151 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
2152 | switch (ch)
|
---|
2153 | {
|
---|
2154 | case VBOXSERVICESESSIONOPT_DOMAIN:
|
---|
2155 | /* Information not needed right now, skip. */
|
---|
2156 | break;
|
---|
2157 | #ifdef DEBUG
|
---|
2158 | case VBOXSERVICESESSIONOPT_DUMP_STDOUT:
|
---|
2159 | fSession |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT;
|
---|
2160 | break;
|
---|
2161 |
|
---|
2162 | case VBOXSERVICESESSIONOPT_DUMP_STDERR:
|
---|
2163 | fSession |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR;
|
---|
2164 | break;
|
---|
2165 | #endif
|
---|
2166 | case VBOXSERVICESESSIONOPT_SESSION_ID:
|
---|
2167 | g_Session.StartupInfo.uSessionID = ValueUnion.u32;
|
---|
2168 | break;
|
---|
2169 |
|
---|
2170 | case VBOXSERVICESESSIONOPT_SESSION_PROTO:
|
---|
2171 | g_Session.StartupInfo.uProtocol = ValueUnion.u32;
|
---|
2172 | break;
|
---|
2173 | #ifdef DEBUG
|
---|
2174 | case VBOXSERVICESESSIONOPT_THREAD_ID:
|
---|
2175 | /* Not handled. Mainly for processs listing. */
|
---|
2176 | break;
|
---|
2177 | #endif
|
---|
2178 | case VBOXSERVICESESSIONOPT_LOG_FILE:
|
---|
2179 | {
|
---|
2180 | int rc = RTStrCopy(g_szLogFile, sizeof(g_szLogFile), ValueUnion.psz);
|
---|
2181 | if (RT_FAILURE(rc))
|
---|
2182 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error copying log file name: %Rrc", rc);
|
---|
2183 | break;
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | case VBOXSERVICESESSIONOPT_USERNAME:
|
---|
2187 | /* Information not needed right now, skip. */
|
---|
2188 | break;
|
---|
2189 |
|
---|
2190 | /** @todo Implement help? */
|
---|
2191 |
|
---|
2192 | case 'v':
|
---|
2193 | g_cVerbosity++;
|
---|
2194 | break;
|
---|
2195 |
|
---|
2196 | case VINF_GETOPT_NOT_OPTION:
|
---|
2197 | /* Ignore; might be "guestsession" main command. */
|
---|
2198 | /** @todo r=bird: We DO NOT ignore stuff on the command line! */
|
---|
2199 | break;
|
---|
2200 |
|
---|
2201 | default:
|
---|
2202 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown command '%s'", ValueUnion.psz);
|
---|
2203 | }
|
---|
2204 | }
|
---|
2205 |
|
---|
2206 | /* Check that we've got all the required options. */
|
---|
2207 | if (g_Session.StartupInfo.uProtocol == UINT32_MAX)
|
---|
2208 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No protocol version specified");
|
---|
2209 |
|
---|
2210 | if (g_Session.StartupInfo.uSessionID == UINT32_MAX)
|
---|
2211 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No session ID specified");
|
---|
2212 |
|
---|
2213 | /* Init the session object. */
|
---|
2214 | int rc = VGSvcGstCtrlSessionInit(&g_Session, fSession);
|
---|
2215 | if (RT_FAILURE(rc))
|
---|
2216 | return RTMsgErrorExit(RTEXITCODE_INIT, "Failed to initialize session object, rc=%Rrc\n", rc);
|
---|
2217 |
|
---|
2218 | rc = VGSvcLogCreate(g_szLogFile[0] ? g_szLogFile : NULL);
|
---|
2219 | if (RT_FAILURE(rc))
|
---|
2220 | return RTMsgErrorExit(RTEXITCODE_INIT, "Failed to create log file '%s', rc=%Rrc\n",
|
---|
2221 | g_szLogFile[0] ? g_szLogFile : "<None>", rc);
|
---|
2222 |
|
---|
2223 | RTEXITCODE rcExit = vgsvcGstCtrlSessionSpawnWorker(&g_Session);
|
---|
2224 |
|
---|
2225 | VGSvcLogDestroy();
|
---|
2226 | return rcExit;
|
---|
2227 | }
|
---|
2228 |
|
---|