1 | /* $Id: VBoxServiceControlSession.cpp 84594 2020-05-28 14:47:27Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxServiceControlSession - Guest session handling. Also handles the spawned session processes.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2013-2020 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 | #include <iprt/rand.h>
|
---|
36 | #include <iprt/system.h> /* For RTShutdown. */
|
---|
37 |
|
---|
38 | #include "VBoxServiceInternal.h"
|
---|
39 | #include "VBoxServiceUtils.h"
|
---|
40 | #include "VBoxServiceControl.h"
|
---|
41 |
|
---|
42 | using namespace guestControl;
|
---|
43 |
|
---|
44 |
|
---|
45 | /*********************************************************************************************************************************
|
---|
46 | * Structures and Typedefs *
|
---|
47 | *********************************************************************************************************************************/
|
---|
48 | /** Generic option indices for session spawn arguments. */
|
---|
49 | enum
|
---|
50 | {
|
---|
51 | VBOXSERVICESESSIONOPT_FIRST = 1000, /* For initialization. */
|
---|
52 | VBOXSERVICESESSIONOPT_DOMAIN,
|
---|
53 | #ifdef DEBUG
|
---|
54 | VBOXSERVICESESSIONOPT_DUMP_STDOUT,
|
---|
55 | VBOXSERVICESESSIONOPT_DUMP_STDERR,
|
---|
56 | #endif
|
---|
57 | VBOXSERVICESESSIONOPT_LOG_FILE,
|
---|
58 | VBOXSERVICESESSIONOPT_USERNAME,
|
---|
59 | VBOXSERVICESESSIONOPT_SESSION_ID,
|
---|
60 | VBOXSERVICESESSIONOPT_SESSION_PROTO,
|
---|
61 | VBOXSERVICESESSIONOPT_THREAD_ID
|
---|
62 | };
|
---|
63 |
|
---|
64 |
|
---|
65 | static int vgsvcGstCtrlSessionCleanupProcesses(const PVBOXSERVICECTRLSESSION pSession);
|
---|
66 | static int vgsvcGstCtrlSessionProcessRemoveInternal(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess);
|
---|
67 |
|
---|
68 |
|
---|
69 | /**
|
---|
70 | * Helper that grows the scratch buffer.
|
---|
71 | * @returns Success indicator.
|
---|
72 | */
|
---|
73 | static bool vgsvcGstCtrlSessionGrowScratchBuf(void **ppvScratchBuf, uint32_t *pcbScratchBuf, uint32_t cbMinBuf)
|
---|
74 | {
|
---|
75 | uint32_t cbNew = *pcbScratchBuf * 2;
|
---|
76 | if ( cbNew <= VMMDEV_MAX_HGCM_DATA_SIZE
|
---|
77 | && cbMinBuf <= VMMDEV_MAX_HGCM_DATA_SIZE)
|
---|
78 | {
|
---|
79 | while (cbMinBuf > cbNew)
|
---|
80 | cbNew *= 2;
|
---|
81 | void *pvNew = RTMemRealloc(*ppvScratchBuf, cbNew);
|
---|
82 | if (pvNew)
|
---|
83 | {
|
---|
84 | *ppvScratchBuf = pvNew;
|
---|
85 | *pcbScratchBuf = cbNew;
|
---|
86 | return true;
|
---|
87 | }
|
---|
88 | }
|
---|
89 | return false;
|
---|
90 | }
|
---|
91 |
|
---|
92 |
|
---|
93 |
|
---|
94 | static int vgsvcGstCtrlSessionFileFree(PVBOXSERVICECTRLFILE pFile)
|
---|
95 | {
|
---|
96 | AssertPtrReturn(pFile, VERR_INVALID_POINTER);
|
---|
97 |
|
---|
98 | int rc = RTFileClose(pFile->hFile);
|
---|
99 | if (RT_SUCCESS(rc))
|
---|
100 | {
|
---|
101 | RTStrFree(pFile->pszName);
|
---|
102 |
|
---|
103 | /* Remove file entry in any case. */
|
---|
104 | RTListNodeRemove(&pFile->Node);
|
---|
105 | /* Destroy this object. */
|
---|
106 | RTMemFree(pFile);
|
---|
107 | }
|
---|
108 |
|
---|
109 | return rc;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /** @todo No locking done yet! */
|
---|
114 | static PVBOXSERVICECTRLFILE vgsvcGstCtrlSessionFileGetLocked(const PVBOXSERVICECTRLSESSION pSession, uint32_t uHandle)
|
---|
115 | {
|
---|
116 | AssertPtrReturn(pSession, NULL);
|
---|
117 |
|
---|
118 | /** @todo Use a map later! */
|
---|
119 | PVBOXSERVICECTRLFILE pFileCur;
|
---|
120 | RTListForEach(&pSession->lstFiles, pFileCur, VBOXSERVICECTRLFILE, Node)
|
---|
121 | {
|
---|
122 | if (pFileCur->uHandle == uHandle)
|
---|
123 | return pFileCur;
|
---|
124 | }
|
---|
125 |
|
---|
126 | return NULL;
|
---|
127 | }
|
---|
128 |
|
---|
129 |
|
---|
130 | /**
|
---|
131 | * Recursion worker for vgsvcGstCtrlSessionHandleDirRemove.
|
---|
132 | * Only (recursively) removes directory structures which are not empty. Will fail if not empty.
|
---|
133 | *
|
---|
134 | * @returns IPRT status code.
|
---|
135 | * @param pszDir The directory buffer, RTPATH_MAX in length.
|
---|
136 | * Contains the abs path to the directory to
|
---|
137 | * recurse into. Trailing slash.
|
---|
138 | * @param cchDir The length of the directory we're recursing into,
|
---|
139 | * including the trailing slash.
|
---|
140 | * @param pDirEntry The dir entry buffer. (Shared to save stack.)
|
---|
141 | */
|
---|
142 | static int vgsvcGstCtrlSessionHandleDirRemoveSub(char *pszDir, size_t cchDir, PRTDIRENTRY pDirEntry)
|
---|
143 | {
|
---|
144 | RTDIR hDir;
|
---|
145 | int rc = RTDirOpen(&hDir, pszDir);
|
---|
146 | if (RT_FAILURE(rc))
|
---|
147 | {
|
---|
148 | /* Ignore non-existing directories like RTDirRemoveRecursive does: */
|
---|
149 | if (rc == VERR_FILE_NOT_FOUND || rc == VERR_PATH_NOT_FOUND)
|
---|
150 | return VINF_SUCCESS;
|
---|
151 | return rc;
|
---|
152 | }
|
---|
153 |
|
---|
154 | for (;;)
|
---|
155 | {
|
---|
156 | rc = RTDirRead(hDir, pDirEntry, NULL);
|
---|
157 | if (RT_FAILURE(rc))
|
---|
158 | {
|
---|
159 | if (rc == VERR_NO_MORE_FILES)
|
---|
160 | rc = VINF_SUCCESS;
|
---|
161 | break;
|
---|
162 | }
|
---|
163 |
|
---|
164 | if (!RTDirEntryIsStdDotLink(pDirEntry))
|
---|
165 | {
|
---|
166 | /* Construct the full name of the entry. */
|
---|
167 | if (cchDir + pDirEntry->cbName + 1 /* dir slash */ < RTPATH_MAX)
|
---|
168 | memcpy(&pszDir[cchDir], pDirEntry->szName, pDirEntry->cbName + 1);
|
---|
169 | else
|
---|
170 | {
|
---|
171 | rc = VERR_FILENAME_TOO_LONG;
|
---|
172 | break;
|
---|
173 | }
|
---|
174 |
|
---|
175 | /* Make sure we've got the entry type. */
|
---|
176 | if (pDirEntry->enmType == RTDIRENTRYTYPE_UNKNOWN)
|
---|
177 | RTDirQueryUnknownType(pszDir, false /*fFollowSymlinks*/, &pDirEntry->enmType);
|
---|
178 |
|
---|
179 | /* Recurse into subdirs and remove them: */
|
---|
180 | if (pDirEntry->enmType == RTDIRENTRYTYPE_DIRECTORY)
|
---|
181 | {
|
---|
182 | size_t cchSubDir = cchDir + pDirEntry->cbName;
|
---|
183 | pszDir[cchSubDir++] = RTPATH_SLASH;
|
---|
184 | pszDir[cchSubDir] = '\0';
|
---|
185 | rc = vgsvcGstCtrlSessionHandleDirRemoveSub(pszDir, cchSubDir, pDirEntry);
|
---|
186 | if (RT_SUCCESS(rc))
|
---|
187 | {
|
---|
188 | pszDir[cchSubDir] = '\0';
|
---|
189 | rc = RTDirRemove(pszDir);
|
---|
190 | if (RT_FAILURE(rc))
|
---|
191 | break;
|
---|
192 | }
|
---|
193 | else
|
---|
194 | break;
|
---|
195 | }
|
---|
196 | /* Not a subdirectory - fail: */
|
---|
197 | else
|
---|
198 | {
|
---|
199 | rc = VERR_DIR_NOT_EMPTY;
|
---|
200 | break;
|
---|
201 | }
|
---|
202 | }
|
---|
203 | }
|
---|
204 |
|
---|
205 | RTDirClose(hDir);
|
---|
206 | return rc;
|
---|
207 | }
|
---|
208 |
|
---|
209 |
|
---|
210 | static int vgsvcGstCtrlSessionHandleDirRemove(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
211 | {
|
---|
212 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
213 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
214 |
|
---|
215 | /*
|
---|
216 | * Retrieve the message.
|
---|
217 | */
|
---|
218 | char szDir[RTPATH_MAX];
|
---|
219 | uint32_t fFlags; /* DIRREMOVE_FLAG_XXX */
|
---|
220 | int rc = VbglR3GuestCtrlDirGetRemove(pHostCtx, szDir, sizeof(szDir), &fFlags);
|
---|
221 | if (RT_SUCCESS(rc))
|
---|
222 | {
|
---|
223 | /*
|
---|
224 | * Do some validating before executing the job.
|
---|
225 | */
|
---|
226 | if (!(fFlags & ~DIRREMOVEREC_FLAG_VALID_MASK))
|
---|
227 | {
|
---|
228 | if (fFlags & DIRREMOVEREC_FLAG_RECURSIVE)
|
---|
229 | {
|
---|
230 | if (fFlags & (DIRREMOVEREC_FLAG_CONTENT_AND_DIR | DIRREMOVEREC_FLAG_CONTENT_ONLY))
|
---|
231 | {
|
---|
232 | uint32_t fFlagsRemRec = fFlags & DIRREMOVEREC_FLAG_CONTENT_AND_DIR
|
---|
233 | ? RTDIRRMREC_F_CONTENT_AND_DIR : RTDIRRMREC_F_CONTENT_ONLY;
|
---|
234 | rc = RTDirRemoveRecursive(szDir, fFlagsRemRec);
|
---|
235 | }
|
---|
236 | else /* Only remove empty directory structures. Will fail if non-empty. */
|
---|
237 | {
|
---|
238 | RTDIRENTRY DirEntry;
|
---|
239 | RTPathEnsureTrailingSeparator(szDir, sizeof(szDir));
|
---|
240 | rc = vgsvcGstCtrlSessionHandleDirRemoveSub(szDir, strlen(szDir), &DirEntry);
|
---|
241 | }
|
---|
242 | VGSvcVerbose(4, "[Dir %s]: rmdir /s (%#x) -> rc=%Rrc\n", szDir, fFlags, rc);
|
---|
243 | }
|
---|
244 | else
|
---|
245 | {
|
---|
246 | /* Only delete directory if not empty. */
|
---|
247 | rc = RTDirRemove(szDir);
|
---|
248 | VGSvcVerbose(4, "[Dir %s]: rmdir (%#x), rc=%Rrc\n", szDir, fFlags, rc);
|
---|
249 | }
|
---|
250 | }
|
---|
251 | else
|
---|
252 | {
|
---|
253 | VGSvcError("[Dir %s]: Unsupported flags: %#x (all %#x)\n", szDir, (fFlags & ~DIRREMOVEREC_FLAG_VALID_MASK), fFlags);
|
---|
254 | rc = VERR_NOT_SUPPORTED;
|
---|
255 | }
|
---|
256 |
|
---|
257 | /*
|
---|
258 | * Report result back to host.
|
---|
259 | */
|
---|
260 | int rc2 = VbglR3GuestCtrlMsgReply(pHostCtx, rc);
|
---|
261 | if (RT_FAILURE(rc2))
|
---|
262 | {
|
---|
263 | VGSvcError("[Dir %s]: Failed to report removing status, rc=%Rrc\n", szDir, rc2);
|
---|
264 | if (RT_SUCCESS(rc))
|
---|
265 | rc = rc2;
|
---|
266 | }
|
---|
267 | }
|
---|
268 | else
|
---|
269 | {
|
---|
270 | VGSvcError("Error fetching parameters for rmdir operation: %Rrc\n", rc);
|
---|
271 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
272 | }
|
---|
273 |
|
---|
274 | VGSvcVerbose(6, "Removing directory '%s' returned rc=%Rrc\n", szDir, rc);
|
---|
275 | return rc;
|
---|
276 | }
|
---|
277 |
|
---|
278 |
|
---|
279 | static int vgsvcGstCtrlSessionHandleFileOpen(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
280 | {
|
---|
281 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
282 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
283 |
|
---|
284 | /*
|
---|
285 | * Retrieve the message.
|
---|
286 | */
|
---|
287 | char szFile[RTPATH_MAX];
|
---|
288 | char szAccess[64];
|
---|
289 | char szDisposition[64];
|
---|
290 | char szSharing[64];
|
---|
291 | uint32_t uCreationMode = 0;
|
---|
292 | uint64_t offOpen = 0;
|
---|
293 | uint32_t uHandle = 0;
|
---|
294 | int rc = VbglR3GuestCtrlFileGetOpen(pHostCtx,
|
---|
295 | /* File to open. */
|
---|
296 | szFile, sizeof(szFile),
|
---|
297 | /* Open mode. */
|
---|
298 | szAccess, sizeof(szAccess),
|
---|
299 | /* Disposition. */
|
---|
300 | szDisposition, sizeof(szDisposition),
|
---|
301 | /* Sharing. */
|
---|
302 | szSharing, sizeof(szSharing),
|
---|
303 | /* Creation mode. */
|
---|
304 | &uCreationMode,
|
---|
305 | /* Offset. */
|
---|
306 | &offOpen);
|
---|
307 | VGSvcVerbose(4, "[File %s]: szAccess=%s, szDisposition=%s, szSharing=%s, offOpen=%RU64, rc=%Rrc\n",
|
---|
308 | szFile, szAccess, szDisposition, szSharing, offOpen, rc);
|
---|
309 | if (RT_SUCCESS(rc))
|
---|
310 | {
|
---|
311 | PVBOXSERVICECTRLFILE pFile = (PVBOXSERVICECTRLFILE)RTMemAllocZ(sizeof(VBOXSERVICECTRLFILE));
|
---|
312 | if (pFile)
|
---|
313 | {
|
---|
314 | pFile->hFile = NIL_RTFILE; /* Not zero or NULL! */
|
---|
315 | if (szFile[0])
|
---|
316 | {
|
---|
317 | pFile->pszName = RTStrDup(szFile);
|
---|
318 | if (!pFile->pszName)
|
---|
319 | rc = VERR_NO_MEMORY;
|
---|
320 | /** @todo
|
---|
321 | * Implement szSharing!
|
---|
322 | */
|
---|
323 | uint64_t fFlags;
|
---|
324 | if (RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | rc = RTFileModeToFlagsEx(szAccess, szDisposition, NULL /* pszSharing, not used yet */, &fFlags);
|
---|
327 | VGSvcVerbose(4, "[File %s] Opening with fFlags=%#RX64 -> rc=%Rrc\n", pFile->pszName, fFlags, rc);
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (RT_SUCCESS(rc))
|
---|
331 | {
|
---|
332 | fFlags |= (uCreationMode << RTFILE_O_CREATE_MODE_SHIFT) & RTFILE_O_CREATE_MODE_MASK;
|
---|
333 | /* If we're opening a file in read-only mode, strip truncation mode.
|
---|
334 | * rtFileRecalcAndValidateFlags() will validate it anyway, but avoid asserting in debug builds. */
|
---|
335 | if (fFlags & RTFILE_O_READ)
|
---|
336 | fFlags &= ~RTFILE_O_TRUNCATE;
|
---|
337 | rc = RTFileOpen(&pFile->hFile, pFile->pszName, fFlags);
|
---|
338 | if (RT_SUCCESS(rc))
|
---|
339 | {
|
---|
340 | RTFSOBJINFO objInfo;
|
---|
341 | rc = RTFileQueryInfo(pFile->hFile, &objInfo, RTFSOBJATTRADD_NOTHING);
|
---|
342 | if (RT_SUCCESS(rc))
|
---|
343 | {
|
---|
344 | /* Make sure that we only open stuff we really support.
|
---|
345 | * Only POSIX / UNIX we could open stuff like directories and sockets as well. */
|
---|
346 | if ( RT_LIKELY(RTFS_IS_FILE(objInfo.Attr.fMode))
|
---|
347 | || RTFS_IS_SYMLINK(objInfo.Attr.fMode))
|
---|
348 | {
|
---|
349 | /* Seeking is optional. However, the whole operation
|
---|
350 | * will fail if we don't succeed seeking to the wanted position. */
|
---|
351 | if (offOpen)
|
---|
352 | rc = RTFileSeek(pFile->hFile, (int64_t)offOpen, RTFILE_SEEK_BEGIN, NULL /* Current offset */);
|
---|
353 | if (RT_SUCCESS(rc))
|
---|
354 | {
|
---|
355 | /*
|
---|
356 | * Succeeded!
|
---|
357 | */
|
---|
358 | uHandle = VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pHostCtx->uContextID);
|
---|
359 | pFile->uHandle = uHandle;
|
---|
360 | pFile->fOpen = fFlags;
|
---|
361 | RTListAppend(&pSession->lstFiles, &pFile->Node);
|
---|
362 | VGSvcVerbose(2, "[File %s] Opened (ID=%RU32)\n", pFile->pszName, pFile->uHandle);
|
---|
363 | }
|
---|
364 | else
|
---|
365 | VGSvcError("[File %s] Seeking to offset %RU64 failed: rc=%Rrc\n", pFile->pszName, offOpen, rc);
|
---|
366 | }
|
---|
367 | else
|
---|
368 | {
|
---|
369 | VGSvcError("[File %s] Unsupported mode %#x\n", pFile->pszName, objInfo.Attr.fMode);
|
---|
370 | rc = VERR_NOT_SUPPORTED;
|
---|
371 | }
|
---|
372 | }
|
---|
373 | else
|
---|
374 | VGSvcError("[File %s] Getting mode failed with rc=%Rrc\n", pFile->pszName, rc);
|
---|
375 | }
|
---|
376 | else
|
---|
377 | VGSvcError("[File %s] Opening failed with rc=%Rrc\n", pFile->pszName, rc);
|
---|
378 | }
|
---|
379 | }
|
---|
380 | else
|
---|
381 | {
|
---|
382 | VGSvcError("[File %s] empty filename!\n", szFile);
|
---|
383 | rc = VERR_INVALID_NAME;
|
---|
384 | }
|
---|
385 |
|
---|
386 | /* clean up if we failed. */
|
---|
387 | if (RT_FAILURE(rc))
|
---|
388 | {
|
---|
389 | RTStrFree(pFile->pszName);
|
---|
390 | if (pFile->hFile != NIL_RTFILE)
|
---|
391 | RTFileClose(pFile->hFile);
|
---|
392 | RTMemFree(pFile);
|
---|
393 | }
|
---|
394 | }
|
---|
395 | else
|
---|
396 | rc = VERR_NO_MEMORY;
|
---|
397 |
|
---|
398 | /*
|
---|
399 | * Report result back to host.
|
---|
400 | */
|
---|
401 | int rc2 = VbglR3GuestCtrlFileCbOpen(pHostCtx, rc, uHandle);
|
---|
402 | if (RT_FAILURE(rc2))
|
---|
403 | {
|
---|
404 | VGSvcError("[File %s]: Failed to report file open status, rc=%Rrc\n", szFile, rc2);
|
---|
405 | if (RT_SUCCESS(rc))
|
---|
406 | rc = rc2;
|
---|
407 | }
|
---|
408 | }
|
---|
409 | else
|
---|
410 | {
|
---|
411 | VGSvcError("Error fetching parameters for open file operation: %Rrc\n", rc);
|
---|
412 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
413 | }
|
---|
414 |
|
---|
415 | VGSvcVerbose(4, "[File %s] Opening (open mode='%s', disposition='%s', creation mode=0x%x) returned rc=%Rrc\n",
|
---|
416 | szFile, szAccess, szDisposition, uCreationMode, rc);
|
---|
417 | return rc;
|
---|
418 | }
|
---|
419 |
|
---|
420 |
|
---|
421 | static int vgsvcGstCtrlSessionHandleFileClose(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
422 | {
|
---|
423 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
424 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
425 |
|
---|
426 | /*
|
---|
427 | * Retrieve the message.
|
---|
428 | */
|
---|
429 | uint32_t uHandle = 0;
|
---|
430 | int rc = VbglR3GuestCtrlFileGetClose(pHostCtx, &uHandle /* File handle to close */);
|
---|
431 | if (RT_SUCCESS(rc))
|
---|
432 | {
|
---|
433 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
434 | if (pFile)
|
---|
435 | {
|
---|
436 | VGSvcVerbose(2, "[File %s] Closing (handle=%RU32)\n", pFile ? pFile->pszName : "<Not found>", uHandle);
|
---|
437 | rc = vgsvcGstCtrlSessionFileFree(pFile);
|
---|
438 | }
|
---|
439 | else
|
---|
440 | {
|
---|
441 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
442 | rc = VERR_NOT_FOUND;
|
---|
443 | }
|
---|
444 |
|
---|
445 | /*
|
---|
446 | * Report result back to host.
|
---|
447 | */
|
---|
448 | int rc2 = VbglR3GuestCtrlFileCbClose(pHostCtx, rc);
|
---|
449 | if (RT_FAILURE(rc2))
|
---|
450 | {
|
---|
451 | VGSvcError("Failed to report file close status, rc=%Rrc\n", rc2);
|
---|
452 | if (RT_SUCCESS(rc))
|
---|
453 | rc = rc2;
|
---|
454 | }
|
---|
455 | }
|
---|
456 | else
|
---|
457 | {
|
---|
458 | VGSvcError("Error fetching parameters for close file operation: %Rrc\n", rc);
|
---|
459 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
460 | }
|
---|
461 | return rc;
|
---|
462 | }
|
---|
463 |
|
---|
464 |
|
---|
465 | static int vgsvcGstCtrlSessionHandleFileRead(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
466 | void **ppvScratchBuf, uint32_t *pcbScratchBuf)
|
---|
467 | {
|
---|
468 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
469 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
470 |
|
---|
471 | /*
|
---|
472 | * Retrieve the request.
|
---|
473 | */
|
---|
474 | uint32_t uHandle = 0;
|
---|
475 | uint32_t cbToRead;
|
---|
476 | int rc = VbglR3GuestCtrlFileGetRead(pHostCtx, &uHandle, &cbToRead);
|
---|
477 | if (RT_SUCCESS(rc))
|
---|
478 | {
|
---|
479 | /*
|
---|
480 | * Locate the file and do the reading.
|
---|
481 | *
|
---|
482 | * If the request is larger than our scratch buffer, try grow it - just
|
---|
483 | * ignore failure as the host better respect our buffer limits.
|
---|
484 | */
|
---|
485 | uint32_t offNew = 0;
|
---|
486 | size_t cbRead = 0;
|
---|
487 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
488 | if (pFile)
|
---|
489 | {
|
---|
490 | if (*pcbScratchBuf < cbToRead)
|
---|
491 | vgsvcGstCtrlSessionGrowScratchBuf(ppvScratchBuf, pcbScratchBuf, cbToRead);
|
---|
492 |
|
---|
493 | rc = RTFileRead(pFile->hFile, *ppvScratchBuf, RT_MIN(cbToRead, *pcbScratchBuf), &cbRead);
|
---|
494 | offNew = (int64_t)RTFileTell(pFile->hFile);
|
---|
495 | VGSvcVerbose(5, "[File %s] Read %zu/%RU32 bytes, rc=%Rrc, offNew=%RI64\n", pFile->pszName, cbRead, cbToRead, rc, offNew);
|
---|
496 | }
|
---|
497 | else
|
---|
498 | {
|
---|
499 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
500 | rc = VERR_NOT_FOUND;
|
---|
501 | }
|
---|
502 |
|
---|
503 | /*
|
---|
504 | * Report result and data back to the host.
|
---|
505 | */
|
---|
506 | int rc2;
|
---|
507 | if (g_fControlHostFeatures0 & VBOX_GUESTCTRL_HF_0_NOTIFY_RDWR_OFFSET)
|
---|
508 | rc2 = VbglR3GuestCtrlFileCbReadOffset(pHostCtx, rc, *ppvScratchBuf, (uint32_t)cbRead, offNew);
|
---|
509 | else
|
---|
510 | rc2 = VbglR3GuestCtrlFileCbRead(pHostCtx, rc, *ppvScratchBuf, (uint32_t)cbRead);
|
---|
511 | if (RT_FAILURE(rc2))
|
---|
512 | {
|
---|
513 | VGSvcError("Failed to report file read status, rc=%Rrc\n", rc2);
|
---|
514 | if (RT_SUCCESS(rc))
|
---|
515 | rc = rc2;
|
---|
516 | }
|
---|
517 | }
|
---|
518 | else
|
---|
519 | {
|
---|
520 | VGSvcError("Error fetching parameters for file read operation: %Rrc\n", rc);
|
---|
521 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
522 | }
|
---|
523 | return rc;
|
---|
524 | }
|
---|
525 |
|
---|
526 |
|
---|
527 | static int vgsvcGstCtrlSessionHandleFileReadAt(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
528 | void **ppvScratchBuf, uint32_t *pcbScratchBuf)
|
---|
529 | {
|
---|
530 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
531 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
532 |
|
---|
533 | /*
|
---|
534 | * Retrieve the request.
|
---|
535 | */
|
---|
536 | uint32_t uHandle = 0;
|
---|
537 | uint32_t cbToRead;
|
---|
538 | uint64_t offReadAt;
|
---|
539 | int rc = VbglR3GuestCtrlFileGetReadAt(pHostCtx, &uHandle, &cbToRead, &offReadAt);
|
---|
540 | if (RT_SUCCESS(rc))
|
---|
541 | {
|
---|
542 | /*
|
---|
543 | * Locate the file and do the reading.
|
---|
544 | *
|
---|
545 | * If the request is larger than our scratch buffer, try grow it - just
|
---|
546 | * ignore failure as the host better respect our buffer limits.
|
---|
547 | */
|
---|
548 | int64_t offNew = 0;
|
---|
549 | size_t cbRead = 0;
|
---|
550 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
551 | if (pFile)
|
---|
552 | {
|
---|
553 | if (*pcbScratchBuf < cbToRead)
|
---|
554 | vgsvcGstCtrlSessionGrowScratchBuf(ppvScratchBuf, pcbScratchBuf, cbToRead);
|
---|
555 |
|
---|
556 | rc = RTFileReadAt(pFile->hFile, (RTFOFF)offReadAt, *ppvScratchBuf, RT_MIN(cbToRead, *pcbScratchBuf), &cbRead);
|
---|
557 | if (RT_SUCCESS(rc))
|
---|
558 | {
|
---|
559 | offNew = offReadAt + cbRead;
|
---|
560 | RTFileSeek(pFile->hFile, offNew, RTFILE_SEEK_BEGIN, NULL); /* RTFileReadAt does not always change position. */
|
---|
561 | }
|
---|
562 | else
|
---|
563 | offNew = (int64_t)RTFileTell(pFile->hFile);
|
---|
564 | VGSvcVerbose(5, "[File %s] Read %zu bytes @ %RU64, rc=%Rrc, offNew=%RI64\n", pFile->pszName, cbRead, offReadAt, rc, offNew);
|
---|
565 | }
|
---|
566 | else
|
---|
567 | {
|
---|
568 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
569 | rc = VERR_NOT_FOUND;
|
---|
570 | }
|
---|
571 |
|
---|
572 | /*
|
---|
573 | * Report result and data back to the host.
|
---|
574 | */
|
---|
575 | int rc2;
|
---|
576 | if (g_fControlHostFeatures0 & VBOX_GUESTCTRL_HF_0_NOTIFY_RDWR_OFFSET)
|
---|
577 | rc2 = VbglR3GuestCtrlFileCbReadOffset(pHostCtx, rc, *ppvScratchBuf, (uint32_t)cbRead, offNew);
|
---|
578 | else
|
---|
579 | rc2 = VbglR3GuestCtrlFileCbRead(pHostCtx, rc, *ppvScratchBuf, (uint32_t)cbRead);
|
---|
580 | if (RT_FAILURE(rc2))
|
---|
581 | {
|
---|
582 | VGSvcError("Failed to report file read at status, rc=%Rrc\n", rc2);
|
---|
583 | if (RT_SUCCESS(rc))
|
---|
584 | rc = rc2;
|
---|
585 | }
|
---|
586 | }
|
---|
587 | else
|
---|
588 | {
|
---|
589 | VGSvcError("Error fetching parameters for file read at operation: %Rrc\n", rc);
|
---|
590 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
591 | }
|
---|
592 | return rc;
|
---|
593 | }
|
---|
594 |
|
---|
595 |
|
---|
596 | static int vgsvcGstCtrlSessionHandleFileWrite(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
597 | void **ppvScratchBuf, uint32_t *pcbScratchBuf)
|
---|
598 | {
|
---|
599 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
600 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * Retrieve the request and data to write.
|
---|
604 | */
|
---|
605 | uint32_t uHandle = 0;
|
---|
606 | uint32_t cbToWrite;
|
---|
607 | int rc = VbglR3GuestCtrlFileGetWrite(pHostCtx, &uHandle, *ppvScratchBuf, *pcbScratchBuf, &cbToWrite);
|
---|
608 | if ( rc == VERR_BUFFER_OVERFLOW
|
---|
609 | && vgsvcGstCtrlSessionGrowScratchBuf(ppvScratchBuf, pcbScratchBuf, cbToWrite))
|
---|
610 | rc = VbglR3GuestCtrlFileGetWrite(pHostCtx, &uHandle, *ppvScratchBuf, *pcbScratchBuf, &cbToWrite);
|
---|
611 | if (RT_SUCCESS(rc))
|
---|
612 | {
|
---|
613 | /*
|
---|
614 | * Locate the file and do the writing.
|
---|
615 | */
|
---|
616 | int64_t offNew = 0;
|
---|
617 | size_t cbWritten = 0;
|
---|
618 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
619 | if (pFile)
|
---|
620 | {
|
---|
621 | rc = RTFileWrite(pFile->hFile, *ppvScratchBuf, RT_MIN(cbToWrite, *pcbScratchBuf), &cbWritten);
|
---|
622 | offNew = (int64_t)RTFileTell(pFile->hFile);
|
---|
623 | VGSvcVerbose(5, "[File %s] Writing %p LB %RU32 => %Rrc, cbWritten=%zu, offNew=%RI64\n",
|
---|
624 | pFile->pszName, *ppvScratchBuf, RT_MIN(cbToWrite, *pcbScratchBuf), rc, cbWritten, offNew);
|
---|
625 | }
|
---|
626 | else
|
---|
627 | {
|
---|
628 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
629 | rc = VERR_NOT_FOUND;
|
---|
630 | }
|
---|
631 |
|
---|
632 | /*
|
---|
633 | * Report result back to host.
|
---|
634 | */
|
---|
635 | int rc2;
|
---|
636 | if (g_fControlHostFeatures0 & VBOX_GUESTCTRL_HF_0_NOTIFY_RDWR_OFFSET)
|
---|
637 | rc2 = VbglR3GuestCtrlFileCbWriteOffset(pHostCtx, rc, (uint32_t)cbWritten, offNew);
|
---|
638 | else
|
---|
639 | rc2 = VbglR3GuestCtrlFileCbWrite(pHostCtx, rc, (uint32_t)cbWritten);
|
---|
640 | if (RT_FAILURE(rc2))
|
---|
641 | {
|
---|
642 | VGSvcError("Failed to report file write status, rc=%Rrc\n", rc2);
|
---|
643 | if (RT_SUCCESS(rc))
|
---|
644 | rc = rc2;
|
---|
645 | }
|
---|
646 | }
|
---|
647 | else
|
---|
648 | {
|
---|
649 | VGSvcError("Error fetching parameters for file write operation: %Rrc\n", rc);
|
---|
650 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
651 | }
|
---|
652 | return rc;
|
---|
653 | }
|
---|
654 |
|
---|
655 |
|
---|
656 | static int vgsvcGstCtrlSessionHandleFileWriteAt(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
657 | void **ppvScratchBuf, uint32_t *pcbScratchBuf)
|
---|
658 | {
|
---|
659 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
660 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
661 |
|
---|
662 | /*
|
---|
663 | * Retrieve the request and data to write.
|
---|
664 | */
|
---|
665 | uint32_t uHandle = 0;
|
---|
666 | uint32_t cbToWrite;
|
---|
667 | uint64_t offWriteAt;
|
---|
668 | int rc = VbglR3GuestCtrlFileGetWriteAt(pHostCtx, &uHandle, *ppvScratchBuf, *pcbScratchBuf, &cbToWrite, &offWriteAt);
|
---|
669 | if ( rc == VERR_BUFFER_OVERFLOW
|
---|
670 | && vgsvcGstCtrlSessionGrowScratchBuf(ppvScratchBuf, pcbScratchBuf, cbToWrite))
|
---|
671 | rc = VbglR3GuestCtrlFileGetWriteAt(pHostCtx, &uHandle, *ppvScratchBuf, *pcbScratchBuf, &cbToWrite, &offWriteAt);
|
---|
672 | if (RT_SUCCESS(rc))
|
---|
673 | {
|
---|
674 | /*
|
---|
675 | * Locate the file and do the writing.
|
---|
676 | */
|
---|
677 | int64_t offNew = 0;
|
---|
678 | size_t cbWritten = 0;
|
---|
679 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
680 | if (pFile)
|
---|
681 | {
|
---|
682 | rc = RTFileWriteAt(pFile->hFile, (RTFOFF)offWriteAt, *ppvScratchBuf, RT_MIN(cbToWrite, *pcbScratchBuf), &cbWritten);
|
---|
683 | if (RT_SUCCESS(rc))
|
---|
684 | {
|
---|
685 | offNew = offWriteAt + cbWritten;
|
---|
686 |
|
---|
687 | /* RTFileWriteAt does not always change position: */
|
---|
688 | if (!(pFile->fOpen & RTFILE_O_APPEND))
|
---|
689 | RTFileSeek(pFile->hFile, offNew, RTFILE_SEEK_BEGIN, NULL);
|
---|
690 | else
|
---|
691 | RTFileSeek(pFile->hFile, 0, RTFILE_SEEK_END, (uint64_t *)&offNew);
|
---|
692 | }
|
---|
693 | else
|
---|
694 | offNew = (int64_t)RTFileTell(pFile->hFile);
|
---|
695 | VGSvcVerbose(5, "[File %s] Writing %p LB %RU32 @ %RU64 => %Rrc, cbWritten=%zu, offNew=%RI64\n",
|
---|
696 | pFile->pszName, *ppvScratchBuf, RT_MIN(cbToWrite, *pcbScratchBuf), offWriteAt, rc, cbWritten, offNew);
|
---|
697 | }
|
---|
698 | else
|
---|
699 | {
|
---|
700 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
701 | rc = VERR_NOT_FOUND;
|
---|
702 | }
|
---|
703 |
|
---|
704 | /*
|
---|
705 | * Report result back to host.
|
---|
706 | */
|
---|
707 | int rc2;
|
---|
708 | if (g_fControlHostFeatures0 & VBOX_GUESTCTRL_HF_0_NOTIFY_RDWR_OFFSET)
|
---|
709 | rc2 = VbglR3GuestCtrlFileCbWriteOffset(pHostCtx, rc, (uint32_t)cbWritten, offNew);
|
---|
710 | else
|
---|
711 | rc2 = VbglR3GuestCtrlFileCbWrite(pHostCtx, rc, (uint32_t)cbWritten);
|
---|
712 | if (RT_FAILURE(rc2))
|
---|
713 | {
|
---|
714 | VGSvcError("Failed to report file write status, rc=%Rrc\n", rc2);
|
---|
715 | if (RT_SUCCESS(rc))
|
---|
716 | rc = rc2;
|
---|
717 | }
|
---|
718 | }
|
---|
719 | else
|
---|
720 | {
|
---|
721 | VGSvcError("Error fetching parameters for file write at operation: %Rrc\n", rc);
|
---|
722 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
723 | }
|
---|
724 | return rc;
|
---|
725 | }
|
---|
726 |
|
---|
727 |
|
---|
728 | static int vgsvcGstCtrlSessionHandleFileSeek(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
729 | {
|
---|
730 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
731 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
732 |
|
---|
733 | /*
|
---|
734 | * Retrieve the request.
|
---|
735 | */
|
---|
736 | uint32_t uHandle = 0;
|
---|
737 | uint32_t uSeekMethod;
|
---|
738 | uint64_t offSeek; /* Will be converted to int64_t. */
|
---|
739 | int rc = VbglR3GuestCtrlFileGetSeek(pHostCtx, &uHandle, &uSeekMethod, &offSeek);
|
---|
740 | if (RT_SUCCESS(rc))
|
---|
741 | {
|
---|
742 | uint64_t offActual = 0;
|
---|
743 |
|
---|
744 | /*
|
---|
745 | * Validate and convert the seek method to IPRT speak.
|
---|
746 | */
|
---|
747 | static const uint8_t s_abMethods[GUEST_FILE_SEEKTYPE_END + 1] =
|
---|
748 | {
|
---|
749 | UINT8_MAX, RTFILE_SEEK_BEGIN, UINT8_MAX, UINT8_MAX, RTFILE_SEEK_CURRENT,
|
---|
750 | UINT8_MAX, UINT8_MAX, UINT8_MAX, RTFILE_SEEK_END
|
---|
751 | };
|
---|
752 | if ( uSeekMethod < RT_ELEMENTS(s_abMethods)
|
---|
753 | && s_abMethods[uSeekMethod] != UINT8_MAX)
|
---|
754 | {
|
---|
755 | /*
|
---|
756 | * Locate the file and do the seek.
|
---|
757 | */
|
---|
758 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
759 | if (pFile)
|
---|
760 | {
|
---|
761 | rc = RTFileSeek(pFile->hFile, (int64_t)offSeek, s_abMethods[uSeekMethod], &offActual);
|
---|
762 | VGSvcVerbose(5, "[File %s]: Seeking to offSeek=%RI64, uSeekMethodIPRT=%u, rc=%Rrc\n",
|
---|
763 | pFile->pszName, offSeek, s_abMethods[uSeekMethod], rc);
|
---|
764 | }
|
---|
765 | else
|
---|
766 | {
|
---|
767 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
768 | rc = VERR_NOT_FOUND;
|
---|
769 | }
|
---|
770 | }
|
---|
771 | else
|
---|
772 | {
|
---|
773 | VGSvcError("Invalid seek method: %#x\n", uSeekMethod);
|
---|
774 | rc = VERR_NOT_SUPPORTED;
|
---|
775 | }
|
---|
776 |
|
---|
777 | /*
|
---|
778 | * Report result back to host.
|
---|
779 | */
|
---|
780 | int rc2 = VbglR3GuestCtrlFileCbSeek(pHostCtx, rc, offActual);
|
---|
781 | if (RT_FAILURE(rc2))
|
---|
782 | {
|
---|
783 | VGSvcError("Failed to report file seek status, rc=%Rrc\n", rc2);
|
---|
784 | if (RT_SUCCESS(rc))
|
---|
785 | rc = rc2;
|
---|
786 | }
|
---|
787 | }
|
---|
788 | else
|
---|
789 | {
|
---|
790 | VGSvcError("Error fetching parameters for file seek operation: %Rrc\n", rc);
|
---|
791 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
792 | }
|
---|
793 | return rc;
|
---|
794 | }
|
---|
795 |
|
---|
796 |
|
---|
797 | static int vgsvcGstCtrlSessionHandleFileTell(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
798 | {
|
---|
799 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
800 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
801 |
|
---|
802 | /*
|
---|
803 | * Retrieve the request.
|
---|
804 | */
|
---|
805 | uint32_t uHandle = 0;
|
---|
806 | int rc = VbglR3GuestCtrlFileGetTell(pHostCtx, &uHandle);
|
---|
807 | if (RT_SUCCESS(rc))
|
---|
808 | {
|
---|
809 | /*
|
---|
810 | * Locate the file and ask for the current position.
|
---|
811 | */
|
---|
812 | uint64_t offCurrent = 0;
|
---|
813 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
814 | if (pFile)
|
---|
815 | {
|
---|
816 | offCurrent = RTFileTell(pFile->hFile);
|
---|
817 | VGSvcVerbose(5, "[File %s]: Telling offCurrent=%RU64\n", pFile->pszName, offCurrent);
|
---|
818 | }
|
---|
819 | else
|
---|
820 | {
|
---|
821 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
822 | rc = VERR_NOT_FOUND;
|
---|
823 | }
|
---|
824 |
|
---|
825 | /*
|
---|
826 | * Report result back to host.
|
---|
827 | */
|
---|
828 | int rc2 = VbglR3GuestCtrlFileCbTell(pHostCtx, rc, offCurrent);
|
---|
829 | if (RT_FAILURE(rc2))
|
---|
830 | {
|
---|
831 | VGSvcError("Failed to report file tell status, rc=%Rrc\n", rc2);
|
---|
832 | if (RT_SUCCESS(rc))
|
---|
833 | rc = rc2;
|
---|
834 | }
|
---|
835 | }
|
---|
836 | else
|
---|
837 | {
|
---|
838 | VGSvcError("Error fetching parameters for file tell operation: %Rrc\n", rc);
|
---|
839 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
840 | }
|
---|
841 | return rc;
|
---|
842 | }
|
---|
843 |
|
---|
844 |
|
---|
845 | static int vgsvcGstCtrlSessionHandleFileSetSize(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
846 | {
|
---|
847 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
848 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
849 |
|
---|
850 | /*
|
---|
851 | * Retrieve the request.
|
---|
852 | */
|
---|
853 | uint32_t uHandle = 0;
|
---|
854 | uint64_t cbNew = 0;
|
---|
855 | int rc = VbglR3GuestCtrlFileGetSetSize(pHostCtx, &uHandle, &cbNew);
|
---|
856 | if (RT_SUCCESS(rc))
|
---|
857 | {
|
---|
858 | /*
|
---|
859 | * Locate the file and ask for the current position.
|
---|
860 | */
|
---|
861 | PVBOXSERVICECTRLFILE pFile = vgsvcGstCtrlSessionFileGetLocked(pSession, uHandle);
|
---|
862 | if (pFile)
|
---|
863 | {
|
---|
864 | rc = RTFileSetSize(pFile->hFile, cbNew);
|
---|
865 | VGSvcVerbose(5, "[File %s]: Changing size to %RU64 (%#RX64), rc=%Rrc\n", pFile->pszName, cbNew, cbNew, rc);
|
---|
866 | }
|
---|
867 | else
|
---|
868 | {
|
---|
869 | VGSvcError("File %u (%#x) not found!\n", uHandle, uHandle);
|
---|
870 | cbNew = UINT64_MAX;
|
---|
871 | rc = VERR_NOT_FOUND;
|
---|
872 | }
|
---|
873 |
|
---|
874 | /*
|
---|
875 | * Report result back to host.
|
---|
876 | */
|
---|
877 | int rc2 = VbglR3GuestCtrlFileCbSetSize(pHostCtx, rc, cbNew);
|
---|
878 | if (RT_FAILURE(rc2))
|
---|
879 | {
|
---|
880 | VGSvcError("Failed to report file tell status, rc=%Rrc\n", rc2);
|
---|
881 | if (RT_SUCCESS(rc))
|
---|
882 | rc = rc2;
|
---|
883 | }
|
---|
884 | }
|
---|
885 | else
|
---|
886 | {
|
---|
887 | VGSvcError("Error fetching parameters for file tell operation: %Rrc\n", rc);
|
---|
888 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
889 | }
|
---|
890 | return rc;
|
---|
891 | }
|
---|
892 |
|
---|
893 |
|
---|
894 | static int vgsvcGstCtrlSessionHandlePathRename(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
895 | {
|
---|
896 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
897 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
898 |
|
---|
899 | /*
|
---|
900 | * Retrieve the request.
|
---|
901 | */
|
---|
902 | char szSource[RTPATH_MAX];
|
---|
903 | char szDest[RTPATH_MAX];
|
---|
904 | uint32_t fFlags = 0; /* PATHRENAME_FLAG_XXX */
|
---|
905 | int rc = VbglR3GuestCtrlPathGetRename(pHostCtx, szSource, sizeof(szSource), szDest, sizeof(szDest), &fFlags);
|
---|
906 | if (RT_SUCCESS(rc))
|
---|
907 | {
|
---|
908 | /*
|
---|
909 | * Validate the flags (kudos for using the same as IPRT), then do the renaming.
|
---|
910 | */
|
---|
911 | AssertCompile(PATHRENAME_FLAG_NO_REPLACE == RTPATHRENAME_FLAGS_NO_REPLACE);
|
---|
912 | AssertCompile(PATHRENAME_FLAG_REPLACE == RTPATHRENAME_FLAGS_REPLACE);
|
---|
913 | AssertCompile(PATHRENAME_FLAG_NO_SYMLINKS == RTPATHRENAME_FLAGS_NO_SYMLINKS);
|
---|
914 | AssertCompile(PATHRENAME_FLAG_VALID_MASK == (RTPATHRENAME_FLAGS_NO_REPLACE | RTPATHRENAME_FLAGS_REPLACE | RTPATHRENAME_FLAGS_NO_SYMLINKS));
|
---|
915 | if (!(fFlags & ~PATHRENAME_FLAG_VALID_MASK))
|
---|
916 | {
|
---|
917 | VGSvcVerbose(4, "Renaming '%s' to '%s', fFlags=%#x, rc=%Rrc\n", szSource, szDest, fFlags, rc);
|
---|
918 | rc = RTPathRename(szSource, szDest, fFlags);
|
---|
919 | }
|
---|
920 | else
|
---|
921 | {
|
---|
922 | VGSvcError("Invalid rename flags: %#x\n", fFlags);
|
---|
923 | rc = VERR_NOT_SUPPORTED;
|
---|
924 | }
|
---|
925 |
|
---|
926 | /*
|
---|
927 | * Report result back to host.
|
---|
928 | */
|
---|
929 | int rc2 = VbglR3GuestCtrlMsgReply(pHostCtx, rc);
|
---|
930 | if (RT_FAILURE(rc2))
|
---|
931 | {
|
---|
932 | VGSvcError("Failed to report renaming status, rc=%Rrc\n", rc2);
|
---|
933 | if (RT_SUCCESS(rc))
|
---|
934 | rc = rc2;
|
---|
935 | }
|
---|
936 | }
|
---|
937 | else
|
---|
938 | {
|
---|
939 | VGSvcError("Error fetching parameters for rename operation: %Rrc\n", rc);
|
---|
940 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
941 | }
|
---|
942 | VGSvcVerbose(5, "Renaming '%s' to '%s' returned rc=%Rrc\n", szSource, szDest, rc);
|
---|
943 | return rc;
|
---|
944 | }
|
---|
945 |
|
---|
946 |
|
---|
947 | /**
|
---|
948 | * Handles getting the user's documents directory.
|
---|
949 | *
|
---|
950 | * @returns VBox status code.
|
---|
951 | * @param pSession Guest session.
|
---|
952 | * @param pHostCtx Host context.
|
---|
953 | */
|
---|
954 | static int vgsvcGstCtrlSessionHandlePathUserDocuments(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
955 | {
|
---|
956 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
957 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
958 |
|
---|
959 | /*
|
---|
960 | * Retrieve the request.
|
---|
961 | */
|
---|
962 | int rc = VbglR3GuestCtrlPathGetUserDocuments(pHostCtx);
|
---|
963 | if (RT_SUCCESS(rc))
|
---|
964 | {
|
---|
965 | /*
|
---|
966 | * Get the path and pass it back to the host..
|
---|
967 | */
|
---|
968 | char szPath[RTPATH_MAX];
|
---|
969 | rc = RTPathUserDocuments(szPath, sizeof(szPath));
|
---|
970 | #ifdef DEBUG
|
---|
971 | VGSvcVerbose(2, "User documents is '%s', rc=%Rrc\n", szPath, rc);
|
---|
972 | #endif
|
---|
973 |
|
---|
974 | int rc2 = VbglR3GuestCtrlMsgReplyEx(pHostCtx, rc, 0 /* Type */, szPath,
|
---|
975 | RT_SUCCESS(rc) ? (uint32_t)strlen(szPath) + 1 /* Include terminating zero */ : 0);
|
---|
976 | if (RT_FAILURE(rc2))
|
---|
977 | {
|
---|
978 | VGSvcError("Failed to report user documents, rc=%Rrc\n", rc2);
|
---|
979 | if (RT_SUCCESS(rc))
|
---|
980 | rc = rc2;
|
---|
981 | }
|
---|
982 | }
|
---|
983 | else
|
---|
984 | {
|
---|
985 | VGSvcError("Error fetching parameters for user documents path request: %Rrc\n", rc);
|
---|
986 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
987 | }
|
---|
988 | return rc;
|
---|
989 | }
|
---|
990 |
|
---|
991 |
|
---|
992 | /**
|
---|
993 | * Handles shutting down / rebooting the guest OS.
|
---|
994 | *
|
---|
995 | * @returns VBox status code.
|
---|
996 | * @param pSession Guest session.
|
---|
997 | * @param pHostCtx Host context.
|
---|
998 | */
|
---|
999 | static int vgsvcGstCtrlSessionHandleShutdown(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1000 | {
|
---|
1001 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1002 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1003 |
|
---|
1004 | /*
|
---|
1005 | * Retrieve the request.
|
---|
1006 | */
|
---|
1007 | uint32_t fAction;
|
---|
1008 | int rc = VbglR3GuestCtrlGetShutdown(pHostCtx, &fAction);
|
---|
1009 | if (RT_SUCCESS(rc))
|
---|
1010 | {
|
---|
1011 | VGSvcVerbose(1, "Host requested to %s system ...\n", (fAction & RTSYSTEM_SHUTDOWN_REBOOT) ? "reboot" : "shutdown");
|
---|
1012 |
|
---|
1013 | /* Reply first to the host, in order to avoid host hangs when issuing the guest shutdown. */
|
---|
1014 | rc = VbglR3GuestCtrlMsgReply(pHostCtx, VINF_SUCCESS);
|
---|
1015 | if (RT_FAILURE(rc))
|
---|
1016 | {
|
---|
1017 | VGSvcError("Failed to reply to shutdown / reboot request, rc=%Rrc\n", rc);
|
---|
1018 | }
|
---|
1019 | else
|
---|
1020 | {
|
---|
1021 | rc = RTSystemShutdown(0 /*cMsDelay*/,
|
---|
1022 | fAction | RTSYSTEM_SHUTDOWN_PLANNED,
|
---|
1023 | "VBoxService");
|
---|
1024 | if (RT_FAILURE(rc))
|
---|
1025 | VGSvcError("%s system failed with %Rrc\n",
|
---|
1026 | (fAction & RTSYSTEM_SHUTDOWN_REBOOT) ? "Rebooting" : "Shutting down", rc);
|
---|
1027 | }
|
---|
1028 | }
|
---|
1029 | else
|
---|
1030 | {
|
---|
1031 | VGSvcError("Error fetching parameters for shutdown / reboot request: %Rrc\n", rc);
|
---|
1032 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1033 | }
|
---|
1034 |
|
---|
1035 | return rc;
|
---|
1036 | }
|
---|
1037 |
|
---|
1038 |
|
---|
1039 | /**
|
---|
1040 | * Handles getting the user's home directory.
|
---|
1041 | *
|
---|
1042 | * @returns VBox status code.
|
---|
1043 | * @param pSession Guest session.
|
---|
1044 | * @param pHostCtx Host context.
|
---|
1045 | */
|
---|
1046 | static int vgsvcGstCtrlSessionHandlePathUserHome(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1047 | {
|
---|
1048 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1049 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1050 |
|
---|
1051 | /*
|
---|
1052 | * Retrieve the request.
|
---|
1053 | */
|
---|
1054 | int rc = VbglR3GuestCtrlPathGetUserHome(pHostCtx);
|
---|
1055 | if (RT_SUCCESS(rc))
|
---|
1056 | {
|
---|
1057 | /*
|
---|
1058 | * Get the path and pass it back to the host..
|
---|
1059 | */
|
---|
1060 | char szPath[RTPATH_MAX];
|
---|
1061 | rc = RTPathUserHome(szPath, sizeof(szPath));
|
---|
1062 |
|
---|
1063 | #ifdef DEBUG
|
---|
1064 | VGSvcVerbose(2, "User home is '%s', rc=%Rrc\n", szPath, rc);
|
---|
1065 | #endif
|
---|
1066 | /* Report back in any case. */
|
---|
1067 | int rc2 = VbglR3GuestCtrlMsgReplyEx(pHostCtx, rc, 0 /* Type */, szPath,
|
---|
1068 | RT_SUCCESS(rc) ?(uint32_t)strlen(szPath) + 1 /* Include terminating zero */ : 0);
|
---|
1069 | if (RT_FAILURE(rc2))
|
---|
1070 | {
|
---|
1071 | VGSvcError("Failed to report user home, rc=%Rrc\n", rc2);
|
---|
1072 | if (RT_SUCCESS(rc))
|
---|
1073 | rc = rc2;
|
---|
1074 | }
|
---|
1075 | }
|
---|
1076 | else
|
---|
1077 | {
|
---|
1078 | VGSvcError("Error fetching parameters for user home directory path request: %Rrc\n", rc);
|
---|
1079 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1080 | }
|
---|
1081 | return rc;
|
---|
1082 | }
|
---|
1083 |
|
---|
1084 | /**
|
---|
1085 | * Handles starting a guest processes.
|
---|
1086 | *
|
---|
1087 | * @returns VBox status code.
|
---|
1088 | * @param pSession Guest session.
|
---|
1089 | * @param pHostCtx Host context.
|
---|
1090 | */
|
---|
1091 | static int vgsvcGstCtrlSessionHandleProcExec(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1092 | {
|
---|
1093 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1094 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1095 |
|
---|
1096 | /** @todo this hardcoded stuff needs redoing. */
|
---|
1097 |
|
---|
1098 | /* Initialize maximum environment block size -- needed as input
|
---|
1099 | * parameter to retrieve the stuff from the host. On output this then
|
---|
1100 | * will contain the actual block size. */
|
---|
1101 | PVBGLR3GUESTCTRLPROCSTARTUPINFO pStartupInfo;
|
---|
1102 | int rc = VbglR3GuestCtrlProcGetStart(pHostCtx, &pStartupInfo);
|
---|
1103 | if (RT_SUCCESS(rc))
|
---|
1104 | {
|
---|
1105 | VGSvcVerbose(3, "Request to start process szCmd=%s, fFlags=0x%x, szArgs=%s, szEnv=%s, uTimeout=%RU32\n",
|
---|
1106 | pStartupInfo->pszCmd, pStartupInfo->fFlags,
|
---|
1107 | pStartupInfo->cArgs ? pStartupInfo->pszArgs : "<None>",
|
---|
1108 | pStartupInfo->cEnvVars ? pStartupInfo->pszEnv : "<None>",
|
---|
1109 | pStartupInfo->uTimeLimitMS);
|
---|
1110 |
|
---|
1111 | bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
|
---|
1112 | rc = VGSvcGstCtrlSessionProcessStartAllowed(pSession, &fStartAllowed);
|
---|
1113 | if (RT_SUCCESS(rc))
|
---|
1114 | {
|
---|
1115 | vgsvcGstCtrlSessionCleanupProcesses(pSession);
|
---|
1116 |
|
---|
1117 | if (fStartAllowed)
|
---|
1118 | rc = VGSvcGstCtrlProcessStart(pSession, pStartupInfo, pHostCtx->uContextID);
|
---|
1119 | else
|
---|
1120 | rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
|
---|
1121 | }
|
---|
1122 |
|
---|
1123 | /* We're responsible for signaling errors to the host (it will wait for ever otherwise). */
|
---|
1124 | if (RT_FAILURE(rc))
|
---|
1125 | {
|
---|
1126 | VGSvcError("Starting process failed with rc=%Rrc, protocol=%RU32, parameters=%RU32\n",
|
---|
1127 | rc, pHostCtx->uProtocol, pHostCtx->uNumParms);
|
---|
1128 | int rc2 = VbglR3GuestCtrlProcCbStatus(pHostCtx, 0 /*nil-PID*/, PROC_STS_ERROR, rc, NULL /*pvData*/, 0 /*cbData*/);
|
---|
1129 | if (RT_FAILURE(rc2))
|
---|
1130 | VGSvcError("Error sending start process status to host, rc=%Rrc\n", rc2);
|
---|
1131 | }
|
---|
1132 |
|
---|
1133 | VbglR3GuestCtrlProcStartupInfoFree(pStartupInfo);
|
---|
1134 | pStartupInfo = NULL;
|
---|
1135 | }
|
---|
1136 | else
|
---|
1137 | {
|
---|
1138 | VGSvcError("Failed to retrieve parameters for process start: %Rrc (cParms=%u)\n", rc, pHostCtx->uNumParms);
|
---|
1139 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1140 | }
|
---|
1141 |
|
---|
1142 | return rc;
|
---|
1143 | }
|
---|
1144 |
|
---|
1145 |
|
---|
1146 | /**
|
---|
1147 | * Sends stdin input to a specific guest process.
|
---|
1148 | *
|
---|
1149 | * @returns VBox status code.
|
---|
1150 | * @param pSession The session which is in charge.
|
---|
1151 | * @param pHostCtx The host context to use.
|
---|
1152 | * @param ppvScratchBuf The scratch buffer, we may grow it.
|
---|
1153 | * @param pcbScratchBuf The scratch buffer size for retrieving the input
|
---|
1154 | * data.
|
---|
1155 | */
|
---|
1156 | static int vgsvcGstCtrlSessionHandleProcInput(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
1157 | void **ppvScratchBuf, uint32_t *pcbScratchBuf)
|
---|
1158 | {
|
---|
1159 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1160 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1161 |
|
---|
1162 | /*
|
---|
1163 | * Retrieve the data from the host.
|
---|
1164 | */
|
---|
1165 | uint32_t uPID;
|
---|
1166 | uint32_t fFlags;
|
---|
1167 | uint32_t cbInput;
|
---|
1168 | int rc = VbglR3GuestCtrlProcGetInput(pHostCtx, &uPID, &fFlags, *ppvScratchBuf, *pcbScratchBuf, &cbInput);
|
---|
1169 | if ( rc == VERR_BUFFER_OVERFLOW
|
---|
1170 | && vgsvcGstCtrlSessionGrowScratchBuf(ppvScratchBuf, pcbScratchBuf, cbInput))
|
---|
1171 | rc = VbglR3GuestCtrlProcGetInput(pHostCtx, &uPID, &fFlags, *ppvScratchBuf, *pcbScratchBuf, &cbInput);
|
---|
1172 | if (RT_SUCCESS(rc))
|
---|
1173 | {
|
---|
1174 | if (fFlags & INPUT_FLAG_EOF)
|
---|
1175 | VGSvcVerbose(4, "Got last process input block for PID=%RU32 (%RU32 bytes) ...\n", uPID, cbInput);
|
---|
1176 |
|
---|
1177 | /*
|
---|
1178 | * Locate the process and feed it.
|
---|
1179 | */
|
---|
1180 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
1181 | if (pProcess)
|
---|
1182 | {
|
---|
1183 | rc = VGSvcGstCtrlProcessHandleInput(pProcess, pHostCtx, RT_BOOL(fFlags & INPUT_FLAG_EOF),
|
---|
1184 | *ppvScratchBuf, RT_MIN(cbInput, *pcbScratchBuf));
|
---|
1185 | if (RT_FAILURE(rc))
|
---|
1186 | VGSvcError("Error handling input message for PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
1187 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
1188 | }
|
---|
1189 | else
|
---|
1190 | {
|
---|
1191 | VGSvcError("Could not find PID %u for feeding %u bytes to it.\n", uPID, cbInput);
|
---|
1192 | rc = VERR_PROCESS_NOT_FOUND;
|
---|
1193 | VbglR3GuestCtrlProcCbStatusInput(pHostCtx, uPID, INPUT_STS_ERROR, rc, 0);
|
---|
1194 | }
|
---|
1195 | }
|
---|
1196 | else
|
---|
1197 | {
|
---|
1198 | VGSvcError("Failed to retrieve parameters for process input: %Rrc (scratch %u bytes)\n", rc, *pcbScratchBuf);
|
---|
1199 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1200 | }
|
---|
1201 |
|
---|
1202 | VGSvcVerbose(6, "Feeding input to PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
1203 | return rc;
|
---|
1204 | }
|
---|
1205 |
|
---|
1206 |
|
---|
1207 | /**
|
---|
1208 | * Gets stdout/stderr output of a specific guest process.
|
---|
1209 | *
|
---|
1210 | * @returns VBox status code.
|
---|
1211 | * @param pSession The session which is in charge.
|
---|
1212 | * @param pHostCtx The host context to use.
|
---|
1213 | */
|
---|
1214 | static int vgsvcGstCtrlSessionHandleProcOutput(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1215 | {
|
---|
1216 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1217 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1218 |
|
---|
1219 | /*
|
---|
1220 | * Retrieve the request.
|
---|
1221 | */
|
---|
1222 | uint32_t uPID;
|
---|
1223 | uint32_t uHandleID;
|
---|
1224 | uint32_t fFlags;
|
---|
1225 | int rc = VbglR3GuestCtrlProcGetOutput(pHostCtx, &uPID, &uHandleID, &fFlags);
|
---|
1226 | #ifdef DEBUG_andy
|
---|
1227 | VGSvcVerbose(4, "Getting output for PID=%RU32, CID=%RU32, uHandleID=%RU32, fFlags=%RU32\n",
|
---|
1228 | uPID, pHostCtx->uContextID, uHandleID, fFlags);
|
---|
1229 | #endif
|
---|
1230 | if (RT_SUCCESS(rc))
|
---|
1231 | {
|
---|
1232 | /*
|
---|
1233 | * Locate the process and hand it the output request.
|
---|
1234 | */
|
---|
1235 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
1236 | if (pProcess)
|
---|
1237 | {
|
---|
1238 | rc = VGSvcGstCtrlProcessHandleOutput(pProcess, pHostCtx, uHandleID, _64K /* cbToRead */, fFlags);
|
---|
1239 | if (RT_FAILURE(rc))
|
---|
1240 | VGSvcError("Error getting output for PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
1241 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
1242 | }
|
---|
1243 | else
|
---|
1244 | {
|
---|
1245 | VGSvcError("Could not find PID %u for draining handle %u (%#x).\n", uPID, uHandleID, uHandleID);
|
---|
1246 | rc = VERR_PROCESS_NOT_FOUND;
|
---|
1247 | /** @todo r=bird:
|
---|
1248 | *
|
---|
1249 | * No way to report status status code for output requests?
|
---|
1250 | *
|
---|
1251 | */
|
---|
1252 | }
|
---|
1253 | }
|
---|
1254 | else
|
---|
1255 | {
|
---|
1256 | VGSvcError("Error fetching parameters for process output request: %Rrc\n", rc);
|
---|
1257 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1258 | }
|
---|
1259 |
|
---|
1260 | #ifdef DEBUG_andy
|
---|
1261 | VGSvcVerbose(4, "Getting output for PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
1262 | #endif
|
---|
1263 | return rc;
|
---|
1264 | }
|
---|
1265 |
|
---|
1266 |
|
---|
1267 | /**
|
---|
1268 | * Tells a guest process to terminate.
|
---|
1269 | *
|
---|
1270 | * @returns VBox status code.
|
---|
1271 | * @param pSession The session which is in charge.
|
---|
1272 | * @param pHostCtx The host context to use.
|
---|
1273 | */
|
---|
1274 | static int vgsvcGstCtrlSessionHandleProcTerminate(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1275 | {
|
---|
1276 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1277 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1278 |
|
---|
1279 | /*
|
---|
1280 | * Retrieve the request.
|
---|
1281 | */
|
---|
1282 | uint32_t uPID;
|
---|
1283 | int rc = VbglR3GuestCtrlProcGetTerminate(pHostCtx, &uPID);
|
---|
1284 | if (RT_SUCCESS(rc))
|
---|
1285 | {
|
---|
1286 | /*
|
---|
1287 | * Locate the process and terminate it.
|
---|
1288 | */
|
---|
1289 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
1290 | if (pProcess)
|
---|
1291 | {
|
---|
1292 | rc = VGSvcGstCtrlProcessHandleTerm(pProcess);
|
---|
1293 | if (RT_FAILURE(rc))
|
---|
1294 | VGSvcError("Error terminating PID=%RU32, rc=%Rrc\n", uPID, rc);
|
---|
1295 |
|
---|
1296 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
1297 | }
|
---|
1298 | else
|
---|
1299 | {
|
---|
1300 | VGSvcError("Could not find PID %u for termination.\n", uPID);
|
---|
1301 | rc = VERR_PROCESS_NOT_FOUND;
|
---|
1302 | }
|
---|
1303 | }
|
---|
1304 | else
|
---|
1305 | {
|
---|
1306 | VGSvcError("Error fetching parameters for process termination request: %Rrc\n", rc);
|
---|
1307 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1308 | }
|
---|
1309 | #ifdef DEBUG_andy
|
---|
1310 | VGSvcVerbose(4, "Terminating PID=%RU32 resulted in rc=%Rrc\n", uPID, rc);
|
---|
1311 | #endif
|
---|
1312 | return rc;
|
---|
1313 | }
|
---|
1314 |
|
---|
1315 |
|
---|
1316 | static int vgsvcGstCtrlSessionHandleProcWaitFor(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLCMDCTX pHostCtx)
|
---|
1317 | {
|
---|
1318 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1319 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1320 |
|
---|
1321 | /*
|
---|
1322 | * Retrieve the request.
|
---|
1323 | */
|
---|
1324 | uint32_t uPID;
|
---|
1325 | uint32_t uWaitFlags;
|
---|
1326 | uint32_t uTimeoutMS;
|
---|
1327 | int rc = VbglR3GuestCtrlProcGetWaitFor(pHostCtx, &uPID, &uWaitFlags, &uTimeoutMS);
|
---|
1328 | if (RT_SUCCESS(rc))
|
---|
1329 | {
|
---|
1330 | /*
|
---|
1331 | * Locate the process and the realize that this call makes no sense
|
---|
1332 | * since we'll notify the host when a process terminates anyway and
|
---|
1333 | * hopefully don't need any additional encouragement.
|
---|
1334 | */
|
---|
1335 | PVBOXSERVICECTRLPROCESS pProcess = VGSvcGstCtrlSessionRetainProcess(pSession, uPID);
|
---|
1336 | if (pProcess)
|
---|
1337 | {
|
---|
1338 | rc = VERR_NOT_IMPLEMENTED; /** @todo */
|
---|
1339 | VGSvcGstCtrlProcessRelease(pProcess);
|
---|
1340 | }
|
---|
1341 | else
|
---|
1342 | rc = VERR_NOT_FOUND;
|
---|
1343 | }
|
---|
1344 | else
|
---|
1345 | {
|
---|
1346 | VGSvcError("Error fetching parameters for process wait request: %Rrc\n", rc);
|
---|
1347 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, rc, UINT32_MAX);
|
---|
1348 | }
|
---|
1349 | return rc;
|
---|
1350 | }
|
---|
1351 |
|
---|
1352 |
|
---|
1353 | int VGSvcGstCtrlSessionHandler(PVBOXSERVICECTRLSESSION pSession, uint32_t uMsg, PVBGLR3GUESTCTRLCMDCTX pHostCtx,
|
---|
1354 | void **ppvScratchBuf, uint32_t *pcbScratchBuf, volatile bool *pfShutdown)
|
---|
1355 | {
|
---|
1356 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1357 | AssertPtrReturn(pHostCtx, VERR_INVALID_POINTER);
|
---|
1358 | AssertPtrReturn(*ppvScratchBuf, VERR_INVALID_POINTER);
|
---|
1359 | AssertPtrReturn(pfShutdown, VERR_INVALID_POINTER);
|
---|
1360 |
|
---|
1361 |
|
---|
1362 | /*
|
---|
1363 | * Only anonymous sessions (that is, sessions which run with local
|
---|
1364 | * service privileges) or spawned session processes can do certain
|
---|
1365 | * operations.
|
---|
1366 | */
|
---|
1367 | bool const fImpersonated = RT_BOOL(pSession->fFlags & ( VBOXSERVICECTRLSESSION_FLAG_SPAWN
|
---|
1368 | | VBOXSERVICECTRLSESSION_FLAG_ANONYMOUS));
|
---|
1369 | int rc = VERR_NOT_SUPPORTED; /* Play safe by default. */
|
---|
1370 |
|
---|
1371 | switch (uMsg)
|
---|
1372 | {
|
---|
1373 | case HOST_MSG_SESSION_CLOSE:
|
---|
1374 | /* Shutdown (this spawn). */
|
---|
1375 | rc = VGSvcGstCtrlSessionClose(pSession);
|
---|
1376 | *pfShutdown = true; /* Shutdown in any case. */
|
---|
1377 | break;
|
---|
1378 |
|
---|
1379 | case HOST_MSG_DIR_REMOVE:
|
---|
1380 | if (fImpersonated)
|
---|
1381 | rc = vgsvcGstCtrlSessionHandleDirRemove(pSession, pHostCtx);
|
---|
1382 | break;
|
---|
1383 |
|
---|
1384 | case HOST_MSG_EXEC_CMD:
|
---|
1385 | rc = vgsvcGstCtrlSessionHandleProcExec(pSession, pHostCtx);
|
---|
1386 | break;
|
---|
1387 |
|
---|
1388 | case HOST_MSG_EXEC_SET_INPUT:
|
---|
1389 | rc = vgsvcGstCtrlSessionHandleProcInput(pSession, pHostCtx, ppvScratchBuf, pcbScratchBuf);
|
---|
1390 | break;
|
---|
1391 |
|
---|
1392 | case HOST_MSG_EXEC_GET_OUTPUT:
|
---|
1393 | rc = vgsvcGstCtrlSessionHandleProcOutput(pSession, pHostCtx);
|
---|
1394 | break;
|
---|
1395 |
|
---|
1396 | case HOST_MSG_EXEC_TERMINATE:
|
---|
1397 | rc = vgsvcGstCtrlSessionHandleProcTerminate(pSession, pHostCtx);
|
---|
1398 | break;
|
---|
1399 |
|
---|
1400 | case HOST_MSG_EXEC_WAIT_FOR:
|
---|
1401 | rc = vgsvcGstCtrlSessionHandleProcWaitFor(pSession, pHostCtx);
|
---|
1402 | break;
|
---|
1403 |
|
---|
1404 | case HOST_MSG_FILE_OPEN:
|
---|
1405 | if (fImpersonated)
|
---|
1406 | rc = vgsvcGstCtrlSessionHandleFileOpen(pSession, pHostCtx);
|
---|
1407 | break;
|
---|
1408 |
|
---|
1409 | case HOST_MSG_FILE_CLOSE:
|
---|
1410 | if (fImpersonated)
|
---|
1411 | rc = vgsvcGstCtrlSessionHandleFileClose(pSession, pHostCtx);
|
---|
1412 | break;
|
---|
1413 |
|
---|
1414 | case HOST_MSG_FILE_READ:
|
---|
1415 | if (fImpersonated)
|
---|
1416 | rc = vgsvcGstCtrlSessionHandleFileRead(pSession, pHostCtx, ppvScratchBuf, pcbScratchBuf);
|
---|
1417 | break;
|
---|
1418 |
|
---|
1419 | case HOST_MSG_FILE_READ_AT:
|
---|
1420 | if (fImpersonated)
|
---|
1421 | rc = vgsvcGstCtrlSessionHandleFileReadAt(pSession, pHostCtx, ppvScratchBuf, pcbScratchBuf);
|
---|
1422 | break;
|
---|
1423 |
|
---|
1424 | case HOST_MSG_FILE_WRITE:
|
---|
1425 | if (fImpersonated)
|
---|
1426 | rc = vgsvcGstCtrlSessionHandleFileWrite(pSession, pHostCtx, ppvScratchBuf, pcbScratchBuf);
|
---|
1427 | break;
|
---|
1428 |
|
---|
1429 | case HOST_MSG_FILE_WRITE_AT:
|
---|
1430 | if (fImpersonated)
|
---|
1431 | rc = vgsvcGstCtrlSessionHandleFileWriteAt(pSession, pHostCtx, ppvScratchBuf, pcbScratchBuf);
|
---|
1432 | break;
|
---|
1433 |
|
---|
1434 | case HOST_MSG_FILE_SEEK:
|
---|
1435 | if (fImpersonated)
|
---|
1436 | rc = vgsvcGstCtrlSessionHandleFileSeek(pSession, pHostCtx);
|
---|
1437 | break;
|
---|
1438 |
|
---|
1439 | case HOST_MSG_FILE_TELL:
|
---|
1440 | if (fImpersonated)
|
---|
1441 | rc = vgsvcGstCtrlSessionHandleFileTell(pSession, pHostCtx);
|
---|
1442 | break;
|
---|
1443 |
|
---|
1444 | case HOST_MSG_FILE_SET_SIZE:
|
---|
1445 | if (fImpersonated)
|
---|
1446 | rc = vgsvcGstCtrlSessionHandleFileSetSize(pSession, pHostCtx);
|
---|
1447 | break;
|
---|
1448 |
|
---|
1449 | case HOST_MSG_PATH_RENAME:
|
---|
1450 | if (fImpersonated)
|
---|
1451 | rc = vgsvcGstCtrlSessionHandlePathRename(pSession, pHostCtx);
|
---|
1452 | break;
|
---|
1453 |
|
---|
1454 | case HOST_MSG_PATH_USER_DOCUMENTS:
|
---|
1455 | if (fImpersonated)
|
---|
1456 | rc = vgsvcGstCtrlSessionHandlePathUserDocuments(pSession, pHostCtx);
|
---|
1457 | break;
|
---|
1458 |
|
---|
1459 | case HOST_MSG_PATH_USER_HOME:
|
---|
1460 | if (fImpersonated)
|
---|
1461 | rc = vgsvcGstCtrlSessionHandlePathUserHome(pSession, pHostCtx);
|
---|
1462 | break;
|
---|
1463 |
|
---|
1464 | case HOST_MSG_SHUTDOWN:
|
---|
1465 | rc = vgsvcGstCtrlSessionHandleShutdown(pSession, pHostCtx);
|
---|
1466 | break;
|
---|
1467 |
|
---|
1468 | default: /* Not supported, see next code block. */
|
---|
1469 | break;
|
---|
1470 | }
|
---|
1471 | if (RT_SUCCESS(rc))
|
---|
1472 | { /* likely */ }
|
---|
1473 | else if (rc != VERR_NOT_SUPPORTED) /* Note: Reply to host must must be sent by above handler. */
|
---|
1474 | VGSvcError("Error while handling message (uMsg=%RU32, cParms=%RU32), rc=%Rrc\n", uMsg, pHostCtx->uNumParms, rc);
|
---|
1475 | else
|
---|
1476 | {
|
---|
1477 | /* We must skip and notify host here as best we can... */
|
---|
1478 | VGSvcVerbose(1, "Unsupported message (uMsg=%RU32, cParms=%RU32) from host, skipping\n", uMsg, pHostCtx->uNumParms);
|
---|
1479 | if (VbglR3GuestCtrlSupportsOptimizations(pHostCtx->uClientID))
|
---|
1480 | VbglR3GuestCtrlMsgSkip(pHostCtx->uClientID, VERR_NOT_SUPPORTED, uMsg);
|
---|
1481 | else
|
---|
1482 | VbglR3GuestCtrlMsgSkipOld(pHostCtx->uClientID);
|
---|
1483 | rc = VINF_SUCCESS;
|
---|
1484 | }
|
---|
1485 |
|
---|
1486 | if (RT_FAILURE(rc))
|
---|
1487 | VGSvcError("Error while handling message (uMsg=%RU32, cParms=%RU32), rc=%Rrc\n", uMsg, pHostCtx->uNumParms, rc);
|
---|
1488 |
|
---|
1489 | return rc;
|
---|
1490 | }
|
---|
1491 |
|
---|
1492 |
|
---|
1493 | /**
|
---|
1494 | * Thread main routine for a spawned guest session process.
|
---|
1495 | *
|
---|
1496 | * This thread runs in the main executable to control the spawned session process.
|
---|
1497 | *
|
---|
1498 | * @returns VBox status code.
|
---|
1499 | * @param hThreadSelf Thread handle.
|
---|
1500 | * @param pvUser Pointer to a VBOXSERVICECTRLSESSIONTHREAD structure.
|
---|
1501 | *
|
---|
1502 | */
|
---|
1503 | static DECLCALLBACK(int) vgsvcGstCtrlSessionThread(RTTHREAD hThreadSelf, void *pvUser)
|
---|
1504 | {
|
---|
1505 | PVBOXSERVICECTRLSESSIONTHREAD pThread = (PVBOXSERVICECTRLSESSIONTHREAD)pvUser;
|
---|
1506 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
1507 |
|
---|
1508 | uint32_t const idSession = pThread->pStartupInfo->uSessionID;
|
---|
1509 | uint32_t const idClient = g_idControlSvcClient;
|
---|
1510 | VGSvcVerbose(3, "Session ID=%RU32 thread running\n", idSession);
|
---|
1511 |
|
---|
1512 | /* Let caller know that we're done initializing, regardless of the result. */
|
---|
1513 | int rc2 = RTThreadUserSignal(hThreadSelf);
|
---|
1514 | AssertRC(rc2);
|
---|
1515 |
|
---|
1516 | /*
|
---|
1517 | * Wait for the child process to stop or the shutdown flag to be signalled.
|
---|
1518 | */
|
---|
1519 | RTPROCSTATUS ProcessStatus = { 0, RTPROCEXITREASON_NORMAL };
|
---|
1520 | bool fProcessAlive = true;
|
---|
1521 | bool fSessionCancelled = VbglR3GuestCtrlSupportsOptimizations(g_idControlSvcClient);
|
---|
1522 | uint32_t cMsShutdownTimeout = 30 * 1000; /** @todo Make this configurable. Later. */
|
---|
1523 | uint64_t msShutdownStart = 0;
|
---|
1524 | uint64_t const msStart = RTTimeMilliTS();
|
---|
1525 | size_t offSecretKey = 0;
|
---|
1526 | int rcWait;
|
---|
1527 | for (;;)
|
---|
1528 | {
|
---|
1529 | /* Secret key feeding. */
|
---|
1530 | if (offSecretKey < sizeof(pThread->abKey))
|
---|
1531 | {
|
---|
1532 | size_t cbWritten = 0;
|
---|
1533 | rc2 = RTPipeWrite(pThread->hKeyPipe, &pThread->abKey[offSecretKey], sizeof(pThread->abKey) - offSecretKey, &cbWritten);
|
---|
1534 | if (RT_SUCCESS(rc2))
|
---|
1535 | offSecretKey += cbWritten;
|
---|
1536 | }
|
---|
1537 |
|
---|
1538 | /* Poll child process status. */
|
---|
1539 | rcWait = RTProcWaitNoResume(pThread->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, &ProcessStatus);
|
---|
1540 | if ( rcWait == VINF_SUCCESS
|
---|
1541 | || rcWait == VERR_PROCESS_NOT_FOUND)
|
---|
1542 | {
|
---|
1543 | fProcessAlive = false;
|
---|
1544 | break;
|
---|
1545 | }
|
---|
1546 | AssertMsgBreak(rcWait == VERR_PROCESS_RUNNING || rcWait == VERR_INTERRUPTED,
|
---|
1547 | ("Got unexpected rc=%Rrc while waiting for session process termination\n", rcWait));
|
---|
1548 |
|
---|
1549 | /* Shutting down? */
|
---|
1550 | if (ASMAtomicReadBool(&pThread->fShutdown))
|
---|
1551 | {
|
---|
1552 | if (!msShutdownStart)
|
---|
1553 | {
|
---|
1554 | VGSvcVerbose(3, "Notifying guest session process (PID=%RU32, session ID=%RU32) ...\n",
|
---|
1555 | pThread->hProcess, idSession);
|
---|
1556 |
|
---|
1557 | VBGLR3GUESTCTRLCMDCTX hostCtx =
|
---|
1558 | {
|
---|
1559 | /* .idClient = */ idClient,
|
---|
1560 | /* .idContext = */ VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(idSession),
|
---|
1561 | /* .uProtocol = */ pThread->pStartupInfo->uProtocol,
|
---|
1562 | /* .cParams = */ 2
|
---|
1563 | };
|
---|
1564 | rc2 = VbglR3GuestCtrlSessionClose(&hostCtx, 0 /* fFlags */);
|
---|
1565 | if (RT_FAILURE(rc2))
|
---|
1566 | {
|
---|
1567 | VGSvcError("Unable to notify guest session process (PID=%RU32, session ID=%RU32), rc=%Rrc\n",
|
---|
1568 | pThread->hProcess, idSession, rc2);
|
---|
1569 |
|
---|
1570 | if (rc2 == VERR_NOT_SUPPORTED)
|
---|
1571 | {
|
---|
1572 | /* Terminate guest session process in case it's not supported by a too old host. */
|
---|
1573 | rc2 = RTProcTerminate(pThread->hProcess);
|
---|
1574 | VGSvcVerbose(3, "Terminating guest session process (PID=%RU32) ended with rc=%Rrc\n",
|
---|
1575 | pThread->hProcess, rc2);
|
---|
1576 | }
|
---|
1577 | break;
|
---|
1578 | }
|
---|
1579 |
|
---|
1580 | VGSvcVerbose(3, "Guest session ID=%RU32 thread was asked to terminate, waiting for session process to exit (%RU32 ms timeout) ...\n",
|
---|
1581 | idSession, cMsShutdownTimeout);
|
---|
1582 | msShutdownStart = RTTimeMilliTS();
|
---|
1583 | continue; /* Don't waste time on waiting. */
|
---|
1584 | }
|
---|
1585 | if (RTTimeMilliTS() - msShutdownStart > cMsShutdownTimeout)
|
---|
1586 | {
|
---|
1587 | VGSvcVerbose(3, "Guest session ID=%RU32 process did not shut down within time\n", idSession);
|
---|
1588 | break;
|
---|
1589 | }
|
---|
1590 | }
|
---|
1591 |
|
---|
1592 | /* Cancel the prepared session stuff after 30 seconds. */
|
---|
1593 | if ( !fSessionCancelled
|
---|
1594 | && RTTimeMilliTS() - msStart >= 30000)
|
---|
1595 | {
|
---|
1596 | VbglR3GuestCtrlSessionCancelPrepared(g_idControlSvcClient, idSession);
|
---|
1597 | fSessionCancelled = true;
|
---|
1598 | }
|
---|
1599 |
|
---|
1600 | /** @todo r=bird: This 100ms sleep is _extremely_ sucky! */
|
---|
1601 | RTThreadSleep(100); /* Wait a bit. */
|
---|
1602 | }
|
---|
1603 |
|
---|
1604 | if (!fSessionCancelled)
|
---|
1605 | VbglR3GuestCtrlSessionCancelPrepared(g_idControlSvcClient, idSession);
|
---|
1606 |
|
---|
1607 | if (!fProcessAlive)
|
---|
1608 | {
|
---|
1609 | VGSvcVerbose(2, "Guest session process (ID=%RU32) terminated with rc=%Rrc, reason=%d, status=%d\n",
|
---|
1610 | idSession, rcWait, ProcessStatus.enmReason, ProcessStatus.iStatus);
|
---|
1611 | if (ProcessStatus.iStatus == RTEXITCODE_INIT)
|
---|
1612 | {
|
---|
1613 | VGSvcError("Guest session process (ID=%RU32) failed to initialize. Here some hints:\n", idSession);
|
---|
1614 | VGSvcError("- Is logging enabled and the output directory is read-only by the guest session user?\n");
|
---|
1615 | /** @todo Add more here. */
|
---|
1616 | }
|
---|
1617 | }
|
---|
1618 |
|
---|
1619 | uint32_t uSessionStatus = GUEST_SESSION_NOTIFYTYPE_UNDEFINED;
|
---|
1620 | uint32_t uSessionRc = VINF_SUCCESS; /** uint32_t vs. int. */
|
---|
1621 |
|
---|
1622 | if (fProcessAlive)
|
---|
1623 | {
|
---|
1624 | for (int i = 0; i < 3; i++)
|
---|
1625 | {
|
---|
1626 | if (i)
|
---|
1627 | RTThreadSleep(3000);
|
---|
1628 |
|
---|
1629 | VGSvcVerbose(2, "Guest session ID=%RU32 process still alive, killing attempt %d/3\n", idSession, i + 1);
|
---|
1630 |
|
---|
1631 | rc2 = RTProcTerminate(pThread->hProcess);
|
---|
1632 | if (RT_SUCCESS(rc2))
|
---|
1633 | break;
|
---|
1634 | }
|
---|
1635 |
|
---|
1636 | VGSvcVerbose(2, "Guest session ID=%RU32 process termination resulted in rc=%Rrc\n", idSession, rc2);
|
---|
1637 | uSessionStatus = RT_SUCCESS(rc2) ? GUEST_SESSION_NOTIFYTYPE_TOK : GUEST_SESSION_NOTIFYTYPE_TOA;
|
---|
1638 | }
|
---|
1639 | else if (RT_SUCCESS(rcWait))
|
---|
1640 | {
|
---|
1641 | switch (ProcessStatus.enmReason)
|
---|
1642 | {
|
---|
1643 | case RTPROCEXITREASON_NORMAL:
|
---|
1644 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEN;
|
---|
1645 | break;
|
---|
1646 |
|
---|
1647 | case RTPROCEXITREASON_ABEND:
|
---|
1648 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEA;
|
---|
1649 | break;
|
---|
1650 |
|
---|
1651 | case RTPROCEXITREASON_SIGNAL:
|
---|
1652 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TES;
|
---|
1653 | break;
|
---|
1654 |
|
---|
1655 | default:
|
---|
1656 | AssertMsgFailed(("Unhandled process termination reason (%d)\n", ProcessStatus.enmReason));
|
---|
1657 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEA;
|
---|
1658 | break;
|
---|
1659 | }
|
---|
1660 | }
|
---|
1661 | else
|
---|
1662 | {
|
---|
1663 | /* If we didn't find the guest process anymore, just assume it terminated normally. */
|
---|
1664 | uSessionStatus = GUEST_SESSION_NOTIFYTYPE_TEN;
|
---|
1665 | }
|
---|
1666 |
|
---|
1667 | /* Make sure to set stopped state before we let the host know. */
|
---|
1668 | ASMAtomicWriteBool(&pThread->fStopped, true);
|
---|
1669 |
|
---|
1670 | /* Report final status, regardless if we failed to wait above, so that the host knows what's going on. */
|
---|
1671 | VGSvcVerbose(3, "Reporting final status %RU32 of session ID=%RU32\n", uSessionStatus, idSession);
|
---|
1672 | Assert(uSessionStatus != GUEST_SESSION_NOTIFYTYPE_UNDEFINED);
|
---|
1673 |
|
---|
1674 | VBGLR3GUESTCTRLCMDCTX ctx = { idClient, VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(idSession),
|
---|
1675 | 0 /* uProtocol, unused */, 0 /* uNumParms, unused */ };
|
---|
1676 | rc2 = VbglR3GuestCtrlSessionNotify(&ctx, uSessionStatus, uSessionRc);
|
---|
1677 | if (RT_FAILURE(rc2))
|
---|
1678 | VGSvcError("Reporting final status of session ID=%RU32 failed with rc=%Rrc\n", idSession, rc2);
|
---|
1679 |
|
---|
1680 | VGSvcVerbose(3, "Thread for session ID=%RU32 ended with sessionStatus=%RU32, sessionRc=%Rrc\n",
|
---|
1681 | idSession, uSessionStatus, uSessionRc);
|
---|
1682 |
|
---|
1683 | return VINF_SUCCESS;
|
---|
1684 | }
|
---|
1685 |
|
---|
1686 | /**
|
---|
1687 | * Reads the secret key the parent VBoxService instance passed us and pass it
|
---|
1688 | * along as a authentication token to the host service.
|
---|
1689 | *
|
---|
1690 | * For older hosts, this sets up the message filtering.
|
---|
1691 | *
|
---|
1692 | * @returns VBox status code.
|
---|
1693 | * @param idClient The HGCM client ID.
|
---|
1694 | * @param idSession The session ID.
|
---|
1695 | */
|
---|
1696 | static int vgsvcGstCtrlSessionReadKeyAndAccept(uint32_t idClient, uint32_t idSession)
|
---|
1697 | {
|
---|
1698 | /*
|
---|
1699 | * Read it.
|
---|
1700 | */
|
---|
1701 | RTHANDLE Handle;
|
---|
1702 | int rc = RTHandleGetStandard(RTHANDLESTD_INPUT, &Handle);
|
---|
1703 | if (RT_SUCCESS(rc))
|
---|
1704 | {
|
---|
1705 | if (Handle.enmType == RTHANDLETYPE_PIPE)
|
---|
1706 | {
|
---|
1707 | uint8_t abSecretKey[RT_SIZEOFMEMB(VBOXSERVICECTRLSESSIONTHREAD, abKey)];
|
---|
1708 | rc = RTPipeReadBlocking(Handle.u.hPipe, abSecretKey, sizeof(abSecretKey), NULL);
|
---|
1709 | if (RT_SUCCESS(rc))
|
---|
1710 | {
|
---|
1711 | VGSvcVerbose(3, "Got secret key from standard input.\n");
|
---|
1712 |
|
---|
1713 | /*
|
---|
1714 | * Do the accepting, if appropriate.
|
---|
1715 | */
|
---|
1716 | if (g_fControlSupportsOptimizations)
|
---|
1717 | {
|
---|
1718 | rc = VbglR3GuestCtrlSessionAccept(idClient, idSession, abSecretKey, sizeof(abSecretKey));
|
---|
1719 | if (RT_SUCCESS(rc))
|
---|
1720 | VGSvcVerbose(3, "Session %u accepted (client ID %u)\n", idClient, idSession);
|
---|
1721 | else
|
---|
1722 | VGSvcError("Failed to accept session %u (client ID %u): %Rrc\n", idClient, idSession, rc);
|
---|
1723 | }
|
---|
1724 | else
|
---|
1725 | {
|
---|
1726 | /* For legacy hosts, we do the filtering thingy. */
|
---|
1727 | rc = VbglR3GuestCtrlMsgFilterSet(idClient, VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(idSession),
|
---|
1728 | VBOX_GUESTCTRL_FILTER_BY_SESSION(idSession), 0);
|
---|
1729 | if (RT_SUCCESS(rc))
|
---|
1730 | VGSvcVerbose(3, "Session %u filtering successfully enabled\n", idSession);
|
---|
1731 | else
|
---|
1732 | VGSvcError("Failed to set session filter: %Rrc\n", rc);
|
---|
1733 | }
|
---|
1734 | }
|
---|
1735 | else
|
---|
1736 | VGSvcError("Error reading secret key from standard input: %Rrc\n", rc);
|
---|
1737 | }
|
---|
1738 | else
|
---|
1739 | {
|
---|
1740 | VGSvcError("Standard input is not a pipe!\n");
|
---|
1741 | rc = VERR_INVALID_HANDLE;
|
---|
1742 | }
|
---|
1743 | RTHandleClose(&Handle);
|
---|
1744 | }
|
---|
1745 | else
|
---|
1746 | VGSvcError("RTHandleGetStandard failed on standard input: %Rrc\n", rc);
|
---|
1747 | return rc;
|
---|
1748 | }
|
---|
1749 |
|
---|
1750 | /**
|
---|
1751 | * Invalidates a guest session by updating all it's internal parameters like host features and stuff.
|
---|
1752 | *
|
---|
1753 | * @param pSession Session to invalidate.
|
---|
1754 | * @param idClient Client ID to use.
|
---|
1755 | */
|
---|
1756 | static void vgsvcGstCtrlSessionInvalidate(PVBOXSERVICECTRLSESSION pSession, uint32_t idClient)
|
---|
1757 | {
|
---|
1758 | RT_NOREF(pSession);
|
---|
1759 |
|
---|
1760 | VGSvcVerbose(1, "Invalidating session %RU32 (client ID=%RU32)\n", idClient, pSession->StartupInfo.uSessionID);
|
---|
1761 |
|
---|
1762 | int rc2 = VbglR3GuestCtrlQueryFeatures(idClient, &g_fControlHostFeatures0);
|
---|
1763 | if (RT_SUCCESS(rc2)) /* Querying host features is not fatal -- do not use rc here. */
|
---|
1764 | {
|
---|
1765 | VGSvcVerbose(1, "g_fControlHostFeatures0=%#x\n", g_fControlHostFeatures0);
|
---|
1766 | }
|
---|
1767 | else
|
---|
1768 | VGSvcVerbose(1, "Querying host features failed with %Rrc\n", rc2);
|
---|
1769 | }
|
---|
1770 |
|
---|
1771 | /**
|
---|
1772 | * Main message handler for the guest control session process.
|
---|
1773 | *
|
---|
1774 | * @returns exit code.
|
---|
1775 | * @param pSession Pointer to g_Session.
|
---|
1776 | * @thread main.
|
---|
1777 | */
|
---|
1778 | static RTEXITCODE vgsvcGstCtrlSessionSpawnWorker(PVBOXSERVICECTRLSESSION pSession)
|
---|
1779 | {
|
---|
1780 | AssertPtrReturn(pSession, RTEXITCODE_FAILURE);
|
---|
1781 | VGSvcVerbose(0, "Hi, this is guest session ID=%RU32\n", pSession->StartupInfo.uSessionID);
|
---|
1782 |
|
---|
1783 | /*
|
---|
1784 | * Connect to the host service.
|
---|
1785 | */
|
---|
1786 | uint32_t idClient;
|
---|
1787 | int rc = VbglR3GuestCtrlConnect(&idClient);
|
---|
1788 | if (RT_FAILURE(rc))
|
---|
1789 | return VGSvcError("Error connecting to guest control service, rc=%Rrc\n", rc);
|
---|
1790 | g_fControlSupportsOptimizations = VbglR3GuestCtrlSupportsOptimizations(idClient);
|
---|
1791 | g_idControlSvcClient = idClient;
|
---|
1792 |
|
---|
1793 | VGSvcVerbose(1, "Using client ID=%RU32\n", idClient);
|
---|
1794 |
|
---|
1795 | vgsvcGstCtrlSessionInvalidate(pSession, idClient);
|
---|
1796 |
|
---|
1797 | rc = vgsvcGstCtrlSessionReadKeyAndAccept(idClient, pSession->StartupInfo.uSessionID);
|
---|
1798 | if (RT_SUCCESS(rc))
|
---|
1799 | {
|
---|
1800 | /*
|
---|
1801 | * Report started status.
|
---|
1802 | * If session status cannot be posted to the host for some reason, bail out.
|
---|
1803 | */
|
---|
1804 | VBGLR3GUESTCTRLCMDCTX ctx = { idClient, VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(pSession->StartupInfo.uSessionID),
|
---|
1805 | 0 /* uProtocol, unused */, 0 /* uNumParms, unused */ };
|
---|
1806 | rc = VbglR3GuestCtrlSessionNotify(&ctx, GUEST_SESSION_NOTIFYTYPE_STARTED, VINF_SUCCESS);
|
---|
1807 | if (RT_SUCCESS(rc))
|
---|
1808 | {
|
---|
1809 | /*
|
---|
1810 | * Allocate a scratch buffer for messages which also send payload data with them.
|
---|
1811 | * This buffer may grow if the host sends us larger chunks of data.
|
---|
1812 | */
|
---|
1813 | uint32_t cbScratchBuf = _64K;
|
---|
1814 | void *pvScratchBuf = RTMemAlloc(cbScratchBuf);
|
---|
1815 | if (pvScratchBuf)
|
---|
1816 | {
|
---|
1817 | int cFailedMsgPeeks = 0;
|
---|
1818 |
|
---|
1819 | /*
|
---|
1820 | * Message processing loop.
|
---|
1821 | */
|
---|
1822 | VBGLR3GUESTCTRLCMDCTX CtxHost = { idClient, 0 /* Context ID */, pSession->StartupInfo.uProtocol, 0 };
|
---|
1823 | for (;;)
|
---|
1824 | {
|
---|
1825 | VGSvcVerbose(3, "Waiting for host msg ...\n");
|
---|
1826 | uint32_t uMsg = 0;
|
---|
1827 | rc = VbglR3GuestCtrlMsgPeekWait(idClient, &uMsg, &CtxHost.uNumParms, NULL);
|
---|
1828 | if (RT_SUCCESS(rc))
|
---|
1829 | {
|
---|
1830 | VGSvcVerbose(4, "Msg=%RU32 (%RU32 parms) retrieved (%Rrc)\n", uMsg, CtxHost.uNumParms, rc);
|
---|
1831 |
|
---|
1832 | /*
|
---|
1833 | * Pass it on to the session handler.
|
---|
1834 | * Note! Only when handling HOST_SESSION_CLOSE is the rc used.
|
---|
1835 | */
|
---|
1836 | bool fShutdown = false;
|
---|
1837 | rc = VGSvcGstCtrlSessionHandler(pSession, uMsg, &CtxHost, &pvScratchBuf, &cbScratchBuf, &fShutdown);
|
---|
1838 | if (fShutdown)
|
---|
1839 | break;
|
---|
1840 |
|
---|
1841 | cFailedMsgPeeks = 0;
|
---|
1842 |
|
---|
1843 | /* Let others run (guests are often single CPU) ... */
|
---|
1844 | RTThreadYield();
|
---|
1845 | }
|
---|
1846 | /*
|
---|
1847 | * Handle restore notification from host. All the context IDs (sessions,
|
---|
1848 | * files, proceses, etc) are invalidated by a VM restore and must be closed.
|
---|
1849 | */
|
---|
1850 | else if (rc == VERR_VM_RESTORED)
|
---|
1851 | {
|
---|
1852 | VGSvcVerbose(1, "The VM session ID changed (i.e. restored)\n");
|
---|
1853 | int rc2 = VGSvcGstCtrlSessionClose(&g_Session);
|
---|
1854 | AssertRC(rc2);
|
---|
1855 |
|
---|
1856 | rc2 = VbglR3GuestCtrlSessionHasChanged(g_idControlSvcClient, g_idControlSvcClient);
|
---|
1857 | AssertRC(rc2);
|
---|
1858 |
|
---|
1859 | /* Invalidate the internal state to match the current host we got restored from. */
|
---|
1860 | vgsvcGstCtrlSessionInvalidate(pSession, g_idControlSvcClient);
|
---|
1861 | }
|
---|
1862 | else
|
---|
1863 | {
|
---|
1864 | VGSvcVerbose(1, "Getting host message failed with %Rrc\n", rc);
|
---|
1865 |
|
---|
1866 | if (cFailedMsgPeeks++ == 3)
|
---|
1867 | break;
|
---|
1868 |
|
---|
1869 | RTThreadSleep(3 * RT_MS_1SEC);
|
---|
1870 |
|
---|
1871 | /** @todo Shouldn't we have a plan for handling connection loss and such? */
|
---|
1872 | }
|
---|
1873 | }
|
---|
1874 |
|
---|
1875 | /*
|
---|
1876 | * Shutdown.
|
---|
1877 | */
|
---|
1878 | RTMemFree(pvScratchBuf);
|
---|
1879 | }
|
---|
1880 | else
|
---|
1881 | rc = VERR_NO_MEMORY;
|
---|
1882 |
|
---|
1883 | VGSvcVerbose(0, "Session %RU32 ended\n", pSession->StartupInfo.uSessionID);
|
---|
1884 | }
|
---|
1885 | else
|
---|
1886 | VGSvcError("Reporting session ID=%RU32 started status failed with rc=%Rrc\n", pSession->StartupInfo.uSessionID, rc);
|
---|
1887 | }
|
---|
1888 | else
|
---|
1889 | VGSvcError("Setting message filterAdd=0x%x failed with rc=%Rrc\n", pSession->StartupInfo.uSessionID, rc);
|
---|
1890 |
|
---|
1891 | VGSvcVerbose(3, "Disconnecting client ID=%RU32 ...\n", idClient);
|
---|
1892 | VbglR3GuestCtrlDisconnect(idClient);
|
---|
1893 | g_idControlSvcClient = 0;
|
---|
1894 |
|
---|
1895 | VGSvcVerbose(3, "Session worker returned with rc=%Rrc\n", rc);
|
---|
1896 | return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1897 | }
|
---|
1898 |
|
---|
1899 |
|
---|
1900 | /**
|
---|
1901 | * Finds a (formerly) started guest process given by its PID and increases its
|
---|
1902 | * reference count.
|
---|
1903 | *
|
---|
1904 | * Must be decreased by the caller with VGSvcGstCtrlProcessRelease().
|
---|
1905 | *
|
---|
1906 | * @returns Guest process if found, otherwise NULL.
|
---|
1907 | * @param pSession Pointer to guest session where to search process in.
|
---|
1908 | * @param uPID PID to search for.
|
---|
1909 | *
|
---|
1910 | * @note This does *not lock the process!
|
---|
1911 | */
|
---|
1912 | PVBOXSERVICECTRLPROCESS VGSvcGstCtrlSessionRetainProcess(PVBOXSERVICECTRLSESSION pSession, uint32_t uPID)
|
---|
1913 | {
|
---|
1914 | AssertPtrReturn(pSession, NULL);
|
---|
1915 |
|
---|
1916 | PVBOXSERVICECTRLPROCESS pProcess = NULL;
|
---|
1917 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1918 | if (RT_SUCCESS(rc))
|
---|
1919 | {
|
---|
1920 | PVBOXSERVICECTRLPROCESS pCurProcess;
|
---|
1921 | RTListForEach(&pSession->lstProcesses, pCurProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
1922 | {
|
---|
1923 | if (pCurProcess->uPID == uPID)
|
---|
1924 | {
|
---|
1925 | rc = RTCritSectEnter(&pCurProcess->CritSect);
|
---|
1926 | if (RT_SUCCESS(rc))
|
---|
1927 | {
|
---|
1928 | pCurProcess->cRefs++;
|
---|
1929 | rc = RTCritSectLeave(&pCurProcess->CritSect);
|
---|
1930 | AssertRC(rc);
|
---|
1931 | }
|
---|
1932 |
|
---|
1933 | if (RT_SUCCESS(rc))
|
---|
1934 | pProcess = pCurProcess;
|
---|
1935 | break;
|
---|
1936 | }
|
---|
1937 | }
|
---|
1938 |
|
---|
1939 | rc = RTCritSectLeave(&pSession->CritSect);
|
---|
1940 | AssertRC(rc);
|
---|
1941 | }
|
---|
1942 |
|
---|
1943 | return pProcess;
|
---|
1944 | }
|
---|
1945 |
|
---|
1946 |
|
---|
1947 | int VGSvcGstCtrlSessionClose(PVBOXSERVICECTRLSESSION pSession)
|
---|
1948 | {
|
---|
1949 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
1950 |
|
---|
1951 | VGSvcVerbose(0, "Session %RU32 is about to close ...\n", pSession->StartupInfo.uSessionID);
|
---|
1952 |
|
---|
1953 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
1954 | if (RT_SUCCESS(rc))
|
---|
1955 | {
|
---|
1956 | /*
|
---|
1957 | * Close all guest processes.
|
---|
1958 | */
|
---|
1959 | VGSvcVerbose(0, "Stopping all guest processes ...\n");
|
---|
1960 |
|
---|
1961 | /* Signal all guest processes in the active list that we want to shutdown. */
|
---|
1962 | PVBOXSERVICECTRLPROCESS pProcess;
|
---|
1963 | RTListForEach(&pSession->lstProcesses, pProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
1964 | VGSvcGstCtrlProcessStop(pProcess);
|
---|
1965 |
|
---|
1966 | VGSvcVerbose(1, "%RU32 guest processes were signalled to stop\n", pSession->cProcesses);
|
---|
1967 |
|
---|
1968 | /* Wait for all active threads to shutdown and destroy the active thread list. */
|
---|
1969 | PVBOXSERVICECTRLPROCESS pProcessNext;
|
---|
1970 | RTListForEachSafe(&pSession->lstProcesses, pProcess, pProcessNext, VBOXSERVICECTRLPROCESS, Node)
|
---|
1971 | {
|
---|
1972 | int rc3 = RTCritSectLeave(&pSession->CritSect);
|
---|
1973 | AssertRC(rc3);
|
---|
1974 |
|
---|
1975 | int rc2 = VGSvcGstCtrlProcessWait(pProcess, 30 * 1000 /* Wait 30 seconds max. */, NULL /* rc */);
|
---|
1976 |
|
---|
1977 | rc3 = RTCritSectEnter(&pSession->CritSect);
|
---|
1978 | AssertRC(rc3);
|
---|
1979 |
|
---|
1980 | if (RT_SUCCESS(rc2))
|
---|
1981 | {
|
---|
1982 | rc2 = vgsvcGstCtrlSessionProcessRemoveInternal(pSession, pProcess);
|
---|
1983 | if (RT_SUCCESS(rc2))
|
---|
1984 | {
|
---|
1985 | VGSvcGstCtrlProcessFree(pProcess);
|
---|
1986 | pProcess = NULL;
|
---|
1987 | }
|
---|
1988 | }
|
---|
1989 | }
|
---|
1990 |
|
---|
1991 | AssertMsg(pSession->cProcesses == 0,
|
---|
1992 | ("Session process list still contains %RU32 when it should not\n", pSession->cProcesses));
|
---|
1993 | AssertMsg(RTListIsEmpty(&pSession->lstProcesses),
|
---|
1994 | ("Session process list is not empty when it should\n"));
|
---|
1995 |
|
---|
1996 | /*
|
---|
1997 | * Close all left guest files.
|
---|
1998 | */
|
---|
1999 | VGSvcVerbose(0, "Closing all guest files ...\n");
|
---|
2000 |
|
---|
2001 | PVBOXSERVICECTRLFILE pFile, pFileNext;
|
---|
2002 | RTListForEachSafe(&pSession->lstFiles, pFile, pFileNext, VBOXSERVICECTRLFILE, Node)
|
---|
2003 | {
|
---|
2004 | int rc2 = vgsvcGstCtrlSessionFileFree(pFile);
|
---|
2005 | if (RT_FAILURE(rc2))
|
---|
2006 | {
|
---|
2007 | VGSvcError("Unable to close file '%s'; rc=%Rrc\n", pFile->pszName, rc2);
|
---|
2008 | if (RT_SUCCESS(rc))
|
---|
2009 | rc = rc2;
|
---|
2010 | /* Keep going. */
|
---|
2011 | }
|
---|
2012 |
|
---|
2013 | pFile = NULL; /* To make it obvious. */
|
---|
2014 | }
|
---|
2015 |
|
---|
2016 | AssertMsg(pSession->cFiles == 0,
|
---|
2017 | ("Session file list still contains %RU32 when it should not\n", pSession->cFiles));
|
---|
2018 | AssertMsg(RTListIsEmpty(&pSession->lstFiles),
|
---|
2019 | ("Session file list is not empty when it should\n"));
|
---|
2020 |
|
---|
2021 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2022 | if (RT_SUCCESS(rc))
|
---|
2023 | rc = rc2;
|
---|
2024 | }
|
---|
2025 |
|
---|
2026 | return rc;
|
---|
2027 | }
|
---|
2028 |
|
---|
2029 |
|
---|
2030 | int VGSvcGstCtrlSessionDestroy(PVBOXSERVICECTRLSESSION pSession)
|
---|
2031 | {
|
---|
2032 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2033 |
|
---|
2034 | int rc = VGSvcGstCtrlSessionClose(pSession);
|
---|
2035 |
|
---|
2036 | /* Destroy critical section. */
|
---|
2037 | RTCritSectDelete(&pSession->CritSect);
|
---|
2038 |
|
---|
2039 | return rc;
|
---|
2040 | }
|
---|
2041 |
|
---|
2042 |
|
---|
2043 | int VGSvcGstCtrlSessionInit(PVBOXSERVICECTRLSESSION pSession, uint32_t fFlags)
|
---|
2044 | {
|
---|
2045 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2046 |
|
---|
2047 | RTListInit(&pSession->lstProcesses);
|
---|
2048 | RTListInit(&pSession->lstFiles);
|
---|
2049 |
|
---|
2050 | pSession->cProcesses = 0;
|
---|
2051 | pSession->cFiles = 0;
|
---|
2052 |
|
---|
2053 | pSession->fFlags = fFlags;
|
---|
2054 |
|
---|
2055 | /* Init critical section for protecting the thread lists. */
|
---|
2056 | int rc = RTCritSectInit(&pSession->CritSect);
|
---|
2057 | AssertRC(rc);
|
---|
2058 |
|
---|
2059 | return rc;
|
---|
2060 | }
|
---|
2061 |
|
---|
2062 |
|
---|
2063 | /**
|
---|
2064 | * Adds a guest process to a session's process list.
|
---|
2065 | *
|
---|
2066 | * @return VBox status code.
|
---|
2067 | * @param pSession Guest session to add process to.
|
---|
2068 | * @param pProcess Guest process to add.
|
---|
2069 | */
|
---|
2070 | int VGSvcGstCtrlSessionProcessAdd(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess)
|
---|
2071 | {
|
---|
2072 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2073 | AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
|
---|
2074 |
|
---|
2075 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
2076 | if (RT_SUCCESS(rc))
|
---|
2077 | {
|
---|
2078 | VGSvcVerbose(3, "Adding process (PID %RU32) to session ID=%RU32\n", pProcess->uPID, pSession->StartupInfo.uSessionID);
|
---|
2079 |
|
---|
2080 | /* Add process to session list. */
|
---|
2081 | RTListAppend(&pSession->lstProcesses, &pProcess->Node);
|
---|
2082 |
|
---|
2083 | pSession->cProcesses++;
|
---|
2084 | VGSvcVerbose(3, "Now session ID=%RU32 has %RU32 processes total\n",
|
---|
2085 | pSession->StartupInfo.uSessionID, pSession->cProcesses);
|
---|
2086 |
|
---|
2087 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2088 | if (RT_SUCCESS(rc))
|
---|
2089 | rc = rc2;
|
---|
2090 | }
|
---|
2091 |
|
---|
2092 | return VINF_SUCCESS;
|
---|
2093 | }
|
---|
2094 |
|
---|
2095 | /**
|
---|
2096 | * Removes a guest process from a session's process list.
|
---|
2097 | * Internal version, does not do locking.
|
---|
2098 | *
|
---|
2099 | * @return VBox status code.
|
---|
2100 | * @param pSession Guest session to remove process from.
|
---|
2101 | * @param pProcess Guest process to remove.
|
---|
2102 | */
|
---|
2103 | static int vgsvcGstCtrlSessionProcessRemoveInternal(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess)
|
---|
2104 | {
|
---|
2105 | VGSvcVerbose(3, "Removing process (PID %RU32) from session ID=%RU32\n", pProcess->uPID, pSession->StartupInfo.uSessionID);
|
---|
2106 | AssertReturn(pProcess->cRefs == 0, VERR_WRONG_ORDER);
|
---|
2107 |
|
---|
2108 | RTListNodeRemove(&pProcess->Node);
|
---|
2109 |
|
---|
2110 | AssertReturn(pSession->cProcesses, VERR_WRONG_ORDER);
|
---|
2111 | pSession->cProcesses--;
|
---|
2112 | VGSvcVerbose(3, "Now session ID=%RU32 has %RU32 processes total\n",
|
---|
2113 | pSession->StartupInfo.uSessionID, pSession->cProcesses);
|
---|
2114 |
|
---|
2115 | return VINF_SUCCESS;
|
---|
2116 | }
|
---|
2117 |
|
---|
2118 | /**
|
---|
2119 | * Removes a guest process from a session's process list.
|
---|
2120 | *
|
---|
2121 | * @return VBox status code.
|
---|
2122 | * @param pSession Guest session to remove process from.
|
---|
2123 | * @param pProcess Guest process to remove.
|
---|
2124 | */
|
---|
2125 | int VGSvcGstCtrlSessionProcessRemove(PVBOXSERVICECTRLSESSION pSession, PVBOXSERVICECTRLPROCESS pProcess)
|
---|
2126 | {
|
---|
2127 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2128 | AssertPtrReturn(pProcess, VERR_INVALID_POINTER);
|
---|
2129 |
|
---|
2130 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
2131 | if (RT_SUCCESS(rc))
|
---|
2132 | {
|
---|
2133 | rc = vgsvcGstCtrlSessionProcessRemoveInternal(pSession, pProcess);
|
---|
2134 |
|
---|
2135 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2136 | if (RT_SUCCESS(rc))
|
---|
2137 | rc = rc2;
|
---|
2138 | }
|
---|
2139 |
|
---|
2140 | return rc;
|
---|
2141 | }
|
---|
2142 |
|
---|
2143 |
|
---|
2144 | /**
|
---|
2145 | * Determines whether starting a new guest process according to the
|
---|
2146 | * maximum number of concurrent guest processes defined is allowed or not.
|
---|
2147 | *
|
---|
2148 | * @return VBox status code.
|
---|
2149 | * @param pSession The guest session.
|
---|
2150 | * @param pfAllowed \c True if starting (another) guest process
|
---|
2151 | * is allowed, \c false if not.
|
---|
2152 | */
|
---|
2153 | int VGSvcGstCtrlSessionProcessStartAllowed(const PVBOXSERVICECTRLSESSION pSession, bool *pfAllowed)
|
---|
2154 | {
|
---|
2155 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2156 | AssertPtrReturn(pfAllowed, VERR_INVALID_POINTER);
|
---|
2157 |
|
---|
2158 | int rc = RTCritSectEnter(&pSession->CritSect);
|
---|
2159 | if (RT_SUCCESS(rc))
|
---|
2160 | {
|
---|
2161 | /*
|
---|
2162 | * Check if we're respecting our memory policy by checking
|
---|
2163 | * how many guest processes are started and served already.
|
---|
2164 | */
|
---|
2165 | bool fLimitReached = false;
|
---|
2166 | if (pSession->uProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
|
---|
2167 | {
|
---|
2168 | VGSvcVerbose(3, "Maximum kept guest processes set to %RU32, acurrent=%RU32\n",
|
---|
2169 | pSession->uProcsMaxKept, pSession->cProcesses);
|
---|
2170 |
|
---|
2171 | int32_t iProcsLeft = (pSession->uProcsMaxKept - pSession->cProcesses - 1);
|
---|
2172 | if (iProcsLeft < 0)
|
---|
2173 | {
|
---|
2174 | VGSvcVerbose(3, "Maximum running guest processes reached (%RU32)\n", pSession->uProcsMaxKept);
|
---|
2175 | fLimitReached = true;
|
---|
2176 | }
|
---|
2177 | }
|
---|
2178 |
|
---|
2179 | *pfAllowed = !fLimitReached;
|
---|
2180 |
|
---|
2181 | int rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2182 | if (RT_SUCCESS(rc))
|
---|
2183 | rc = rc2;
|
---|
2184 | }
|
---|
2185 |
|
---|
2186 | return rc;
|
---|
2187 | }
|
---|
2188 |
|
---|
2189 |
|
---|
2190 | /**
|
---|
2191 | * Cleans up stopped and no longer used processes.
|
---|
2192 | *
|
---|
2193 | * This will free and remove processes from the session's process list.
|
---|
2194 | *
|
---|
2195 | * @returns VBox status code.
|
---|
2196 | * @param pSession Session to clean up processes for.
|
---|
2197 | */
|
---|
2198 | static int vgsvcGstCtrlSessionCleanupProcesses(const PVBOXSERVICECTRLSESSION pSession)
|
---|
2199 | {
|
---|
2200 | AssertPtrReturn(pSession, VERR_INVALID_POINTER);
|
---|
2201 |
|
---|
2202 | VGSvcVerbose(3, "Cleaning up stopped processes for session %RU32 ...\n", pSession->StartupInfo.uSessionID);
|
---|
2203 |
|
---|
2204 | int rc2 = RTCritSectEnter(&pSession->CritSect);
|
---|
2205 | AssertRC(rc2);
|
---|
2206 |
|
---|
2207 | int rc = VINF_SUCCESS;
|
---|
2208 |
|
---|
2209 | PVBOXSERVICECTRLPROCESS pCurProcess, pNextProcess;
|
---|
2210 | RTListForEachSafe(&pSession->lstProcesses, pCurProcess, pNextProcess, VBOXSERVICECTRLPROCESS, Node)
|
---|
2211 | {
|
---|
2212 | if (ASMAtomicReadBool(&pCurProcess->fStopped))
|
---|
2213 | {
|
---|
2214 | rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2215 | AssertRC(rc2);
|
---|
2216 |
|
---|
2217 | rc = VGSvcGstCtrlProcessWait(pCurProcess, 30 * 1000 /* Wait 30 seconds max. */, NULL /* rc */);
|
---|
2218 | if (RT_SUCCESS(rc))
|
---|
2219 | {
|
---|
2220 | VGSvcGstCtrlSessionProcessRemove(pSession, pCurProcess);
|
---|
2221 | VGSvcGstCtrlProcessFree(pCurProcess);
|
---|
2222 | }
|
---|
2223 |
|
---|
2224 | rc2 = RTCritSectEnter(&pSession->CritSect);
|
---|
2225 | AssertRC(rc2);
|
---|
2226 |
|
---|
2227 | /* If failed, try next time we're being called. */
|
---|
2228 | }
|
---|
2229 | }
|
---|
2230 |
|
---|
2231 | rc2 = RTCritSectLeave(&pSession->CritSect);
|
---|
2232 | AssertRC(rc2);
|
---|
2233 |
|
---|
2234 | if (RT_FAILURE(rc))
|
---|
2235 | VGSvcError("Cleaning up stopped processes for session %RU32 failed with %Rrc\n", pSession->StartupInfo.uSessionID, rc);
|
---|
2236 |
|
---|
2237 | return rc;
|
---|
2238 | }
|
---|
2239 |
|
---|
2240 |
|
---|
2241 | /**
|
---|
2242 | * Creates the process for a guest session.
|
---|
2243 | *
|
---|
2244 | * @return VBox status code.
|
---|
2245 | * @param pSessionStartupInfo Session startup info.
|
---|
2246 | * @param pSessionThread The session thread under construction.
|
---|
2247 | * @param uCtrlSessionThread The session thread debug ordinal.
|
---|
2248 | */
|
---|
2249 | static int vgsvcVGSvcGstCtrlSessionThreadCreateProcess(const PVBGLR3GUESTCTRLSESSIONSTARTUPINFO pSessionStartupInfo,
|
---|
2250 | PVBOXSERVICECTRLSESSIONTHREAD pSessionThread, uint32_t uCtrlSessionThread)
|
---|
2251 | {
|
---|
2252 | RT_NOREF(uCtrlSessionThread);
|
---|
2253 |
|
---|
2254 | /*
|
---|
2255 | * Is this an anonymous session? Anonymous sessions run with the same
|
---|
2256 | * privileges as the main VBoxService executable.
|
---|
2257 | */
|
---|
2258 | bool const fAnonymous = pSessionThread->pStartupInfo->pszUser
|
---|
2259 | && pSessionThread->pStartupInfo->pszUser[0] == '\0';
|
---|
2260 | if (fAnonymous)
|
---|
2261 | {
|
---|
2262 | Assert(!strlen(pSessionThread->pStartupInfo->pszPassword));
|
---|
2263 | Assert(!strlen(pSessionThread->pStartupInfo->pszDomain));
|
---|
2264 |
|
---|
2265 | VGSvcVerbose(3, "New anonymous guest session ID=%RU32 created, fFlags=%x, using protocol %RU32\n",
|
---|
2266 | pSessionStartupInfo->uSessionID,
|
---|
2267 | pSessionStartupInfo->fFlags,
|
---|
2268 | pSessionStartupInfo->uProtocol);
|
---|
2269 | }
|
---|
2270 | else
|
---|
2271 | {
|
---|
2272 | VGSvcVerbose(3, "Spawning new guest session ID=%RU32, szUser=%s, szPassword=%s, szDomain=%s, fFlags=%x, using protocol %RU32\n",
|
---|
2273 | pSessionStartupInfo->uSessionID,
|
---|
2274 | pSessionStartupInfo->pszUser,
|
---|
2275 | #ifdef DEBUG
|
---|
2276 | pSessionStartupInfo->pszPassword,
|
---|
2277 | #else
|
---|
2278 | "XXX", /* Never show passwords in release mode. */
|
---|
2279 | #endif
|
---|
2280 | pSessionStartupInfo->pszDomain,
|
---|
2281 | pSessionStartupInfo->fFlags,
|
---|
2282 | pSessionStartupInfo->uProtocol);
|
---|
2283 | }
|
---|
2284 |
|
---|
2285 | /*
|
---|
2286 | * Spawn a child process for doing the actual session handling.
|
---|
2287 | * Start by assembling the argument list.
|
---|
2288 | */
|
---|
2289 | char szExeName[RTPATH_MAX];
|
---|
2290 | char *pszExeName = RTProcGetExecutablePath(szExeName, sizeof(szExeName));
|
---|
2291 | AssertReturn(pszExeName, VERR_FILENAME_TOO_LONG);
|
---|
2292 |
|
---|
2293 | char szParmSessionID[32];
|
---|
2294 | RTStrPrintf(szParmSessionID, sizeof(szParmSessionID), "--session-id=%RU32", pSessionThread->pStartupInfo->uSessionID);
|
---|
2295 |
|
---|
2296 | char szParmSessionProto[32];
|
---|
2297 | RTStrPrintf(szParmSessionProto, sizeof(szParmSessionProto), "--session-proto=%RU32",
|
---|
2298 | pSessionThread->pStartupInfo->uProtocol);
|
---|
2299 | #ifdef DEBUG
|
---|
2300 | char szParmThreadId[32];
|
---|
2301 | RTStrPrintf(szParmThreadId, sizeof(szParmThreadId), "--thread-id=%RU32", uCtrlSessionThread);
|
---|
2302 | #endif
|
---|
2303 | unsigned idxArg = 0; /* Next index in argument vector. */
|
---|
2304 | char const *apszArgs[24];
|
---|
2305 |
|
---|
2306 | apszArgs[idxArg++] = pszExeName;
|
---|
2307 | apszArgs[idxArg++] = "guestsession";
|
---|
2308 | apszArgs[idxArg++] = szParmSessionID;
|
---|
2309 | apszArgs[idxArg++] = szParmSessionProto;
|
---|
2310 | #ifdef DEBUG
|
---|
2311 | apszArgs[idxArg++] = szParmThreadId;
|
---|
2312 | #endif
|
---|
2313 | if (!fAnonymous) /* Do we need to pass a user name? */
|
---|
2314 | {
|
---|
2315 | apszArgs[idxArg++] = "--user";
|
---|
2316 | apszArgs[idxArg++] = pSessionThread->pStartupInfo->pszUser;
|
---|
2317 |
|
---|
2318 | if (strlen(pSessionThread->pStartupInfo->pszDomain))
|
---|
2319 | {
|
---|
2320 | apszArgs[idxArg++] = "--domain";
|
---|
2321 | apszArgs[idxArg++] = pSessionThread->pStartupInfo->pszDomain;
|
---|
2322 | }
|
---|
2323 | }
|
---|
2324 |
|
---|
2325 | /* Add same verbose flags as parent process. */
|
---|
2326 | char szParmVerbose[32];
|
---|
2327 | if (g_cVerbosity > 0)
|
---|
2328 | {
|
---|
2329 | unsigned cVs = RT_MIN(g_cVerbosity, RT_ELEMENTS(szParmVerbose) - 2);
|
---|
2330 | szParmVerbose[0] = '-';
|
---|
2331 | memset(&szParmVerbose[1], 'v', cVs);
|
---|
2332 | szParmVerbose[1 + cVs] = '\0';
|
---|
2333 | apszArgs[idxArg++] = szParmVerbose;
|
---|
2334 | }
|
---|
2335 |
|
---|
2336 | /* Add log file handling. Each session will have an own
|
---|
2337 | * log file, naming based on the parent log file. */
|
---|
2338 | char szParmLogFile[sizeof(g_szLogFile) + 128];
|
---|
2339 | if (g_szLogFile[0])
|
---|
2340 | {
|
---|
2341 | const char *pszSuffix = RTPathSuffix(g_szLogFile);
|
---|
2342 | if (!pszSuffix)
|
---|
2343 | pszSuffix = strchr(g_szLogFile, '\0');
|
---|
2344 | size_t cchBase = pszSuffix - g_szLogFile;
|
---|
2345 |
|
---|
2346 | RTTIMESPEC Now;
|
---|
2347 | RTTimeNow(&Now);
|
---|
2348 | char szTime[64];
|
---|
2349 | RTTimeSpecToString(&Now, szTime, sizeof(szTime));
|
---|
2350 |
|
---|
2351 | /* Replace out characters not allowed on Windows platforms, put in by RTTimeSpecToString(). */
|
---|
2352 | static const RTUNICP s_uszValidRangePairs[] =
|
---|
2353 | {
|
---|
2354 | ' ', ' ',
|
---|
2355 | '(', ')',
|
---|
2356 | '-', '.',
|
---|
2357 | '0', '9',
|
---|
2358 | 'A', 'Z',
|
---|
2359 | 'a', 'z',
|
---|
2360 | '_', '_',
|
---|
2361 | 0xa0, 0xd7af,
|
---|
2362 | '\0'
|
---|
2363 | };
|
---|
2364 | ssize_t cReplaced = RTStrPurgeComplementSet(szTime, s_uszValidRangePairs, '_' /* chReplacement */);
|
---|
2365 | AssertReturn(cReplaced, VERR_INVALID_UTF8_ENCODING);
|
---|
2366 |
|
---|
2367 | #ifndef DEBUG
|
---|
2368 | RTStrPrintf(szParmLogFile, sizeof(szParmLogFile), "%.*s-%RU32-%s-%s%s",
|
---|
2369 | cchBase, g_szLogFile, pSessionStartupInfo->uSessionID, pSessionStartupInfo->pszUser, szTime, pszSuffix);
|
---|
2370 | #else
|
---|
2371 | RTStrPrintf(szParmLogFile, sizeof(szParmLogFile), "%.*s-%RU32-%RU32-%s-%s%s",
|
---|
2372 | cchBase, g_szLogFile, pSessionStartupInfo->uSessionID, uCtrlSessionThread,
|
---|
2373 | pSessionStartupInfo->pszUser, szTime, pszSuffix);
|
---|
2374 | #endif
|
---|
2375 | apszArgs[idxArg++] = "--logfile";
|
---|
2376 | apszArgs[idxArg++] = szParmLogFile;
|
---|
2377 | }
|
---|
2378 |
|
---|
2379 | #ifdef DEBUG
|
---|
2380 | if (g_Session.fFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT)
|
---|
2381 | apszArgs[idxArg++] = "--dump-stdout";
|
---|
2382 | if (g_Session.fFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR)
|
---|
2383 | apszArgs[idxArg++] = "--dump-stderr";
|
---|
2384 | #endif
|
---|
2385 | apszArgs[idxArg] = NULL;
|
---|
2386 | Assert(idxArg < RT_ELEMENTS(apszArgs));
|
---|
2387 |
|
---|
2388 | if (g_cVerbosity > 3)
|
---|
2389 | {
|
---|
2390 | VGSvcVerbose(4, "Spawning parameters:\n");
|
---|
2391 | for (idxArg = 0; apszArgs[idxArg]; idxArg++)
|
---|
2392 | VGSvcVerbose(4, " %s\n", apszArgs[idxArg]);
|
---|
2393 | }
|
---|
2394 |
|
---|
2395 | /*
|
---|
2396 | * Flags.
|
---|
2397 | */
|
---|
2398 | uint32_t const fProcCreate = RTPROC_FLAGS_PROFILE
|
---|
2399 | #ifdef RT_OS_WINDOWS
|
---|
2400 | | RTPROC_FLAGS_SERVICE
|
---|
2401 | | RTPROC_FLAGS_HIDDEN
|
---|
2402 | #endif
|
---|
2403 | ;
|
---|
2404 |
|
---|
2405 | /*
|
---|
2406 | * Configure standard handles.
|
---|
2407 | */
|
---|
2408 | RTHANDLE hStdIn;
|
---|
2409 | int rc = RTPipeCreate(&hStdIn.u.hPipe, &pSessionThread->hKeyPipe, RTPIPE_C_INHERIT_READ);
|
---|
2410 | if (RT_SUCCESS(rc))
|
---|
2411 | {
|
---|
2412 | hStdIn.enmType = RTHANDLETYPE_PIPE;
|
---|
2413 |
|
---|
2414 | RTHANDLE hStdOutAndErr;
|
---|
2415 | rc = RTFileOpenBitBucket(&hStdOutAndErr.u.hFile, RTFILE_O_WRITE);
|
---|
2416 | if (RT_SUCCESS(rc))
|
---|
2417 | {
|
---|
2418 | hStdOutAndErr.enmType = RTHANDLETYPE_FILE;
|
---|
2419 |
|
---|
2420 | /*
|
---|
2421 | * Windows: If a domain name is given, construct an UPN (User Principle Name)
|
---|
2422 | * with the domain name built-in, e.g. "[email protected]".
|
---|
2423 | */
|
---|
2424 | const char *pszUser = pSessionThread->pStartupInfo->pszUser;
|
---|
2425 | #ifdef RT_OS_WINDOWS
|
---|
2426 | char *pszUserUPN = NULL;
|
---|
2427 | if (pSessionThread->pStartupInfo->pszDomain[0])
|
---|
2428 | {
|
---|
2429 | int cchbUserUPN = RTStrAPrintf(&pszUserUPN, "%s@%s",
|
---|
2430 | pSessionThread->pStartupInfo->pszUser,
|
---|
2431 | pSessionThread->pStartupInfo->pszDomain);
|
---|
2432 | if (cchbUserUPN > 0)
|
---|
2433 | {
|
---|
2434 | pszUser = pszUserUPN;
|
---|
2435 | VGSvcVerbose(3, "Using UPN: %s\n", pszUserUPN);
|
---|
2436 | }
|
---|
2437 | else
|
---|
2438 | rc = VERR_NO_STR_MEMORY;
|
---|
2439 | }
|
---|
2440 | if (RT_SUCCESS(rc))
|
---|
2441 | #endif
|
---|
2442 | {
|
---|
2443 | /*
|
---|
2444 | * Finally, create the process.
|
---|
2445 | */
|
---|
2446 | rc = RTProcCreateEx(pszExeName, apszArgs, RTENV_DEFAULT, fProcCreate,
|
---|
2447 | &hStdIn, &hStdOutAndErr, &hStdOutAndErr,
|
---|
2448 | !fAnonymous ? pszUser : NULL,
|
---|
2449 | !fAnonymous ? pSessionThread->pStartupInfo->pszPassword : NULL,
|
---|
2450 | NULL /*pvExtraData*/,
|
---|
2451 | &pSessionThread->hProcess);
|
---|
2452 | }
|
---|
2453 | #ifdef RT_OS_WINDOWS
|
---|
2454 | RTStrFree(pszUserUPN);
|
---|
2455 | #endif
|
---|
2456 | RTFileClose(hStdOutAndErr.u.hFile);
|
---|
2457 | }
|
---|
2458 |
|
---|
2459 | RTPipeClose(hStdIn.u.hPipe);
|
---|
2460 | }
|
---|
2461 | return rc;
|
---|
2462 | }
|
---|
2463 |
|
---|
2464 |
|
---|
2465 | /**
|
---|
2466 | * Creates a guest session.
|
---|
2467 | *
|
---|
2468 | * This will spawn a new VBoxService.exe instance under behalf of the given user
|
---|
2469 | * which then will act as a session host. On successful open, the session will
|
---|
2470 | * be added to the given session thread list.
|
---|
2471 | *
|
---|
2472 | * @return VBox status code.
|
---|
2473 | * @param pList Which list to use to store the session thread in.
|
---|
2474 | * @param pSessionStartupInfo Session startup info.
|
---|
2475 | * @param ppSessionThread Returns newly created session thread on success.
|
---|
2476 | * Optional.
|
---|
2477 | */
|
---|
2478 | int VGSvcGstCtrlSessionThreadCreate(PRTLISTANCHOR pList, const PVBGLR3GUESTCTRLSESSIONSTARTUPINFO pSessionStartupInfo,
|
---|
2479 | PVBOXSERVICECTRLSESSIONTHREAD *ppSessionThread)
|
---|
2480 | {
|
---|
2481 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
2482 | AssertPtrReturn(pSessionStartupInfo, VERR_INVALID_POINTER);
|
---|
2483 | /* ppSessionThread is optional. */
|
---|
2484 |
|
---|
2485 | #ifdef VBOX_STRICT
|
---|
2486 | /* Check for existing session in debug mode. Should never happen because of
|
---|
2487 | * Main consistency. */
|
---|
2488 | PVBOXSERVICECTRLSESSIONTHREAD pSessionCur;
|
---|
2489 | RTListForEach(pList, pSessionCur, VBOXSERVICECTRLSESSIONTHREAD, Node)
|
---|
2490 | {
|
---|
2491 | AssertMsgReturn( pSessionCur->fStopped == true
|
---|
2492 | || pSessionCur->pStartupInfo->uSessionID != pSessionStartupInfo->uSessionID,
|
---|
2493 | ("Guest session thread ID=%RU32 already exists (fStopped=%RTbool)\n",
|
---|
2494 | pSessionCur->pStartupInfo->uSessionID, pSessionCur->fStopped), VERR_ALREADY_EXISTS);
|
---|
2495 | }
|
---|
2496 | #endif
|
---|
2497 |
|
---|
2498 | /* Static counter to help tracking session thread <-> process relations. */
|
---|
2499 | static uint32_t s_uCtrlSessionThread = 0;
|
---|
2500 |
|
---|
2501 | /*
|
---|
2502 | * Allocate and initialize the session thread structure.
|
---|
2503 | */
|
---|
2504 | int rc;
|
---|
2505 | PVBOXSERVICECTRLSESSIONTHREAD pSessionThread = (PVBOXSERVICECTRLSESSIONTHREAD)RTMemAllocZ(sizeof(*pSessionThread));
|
---|
2506 | if (pSessionThread)
|
---|
2507 | {
|
---|
2508 | //pSessionThread->fShutdown = false;
|
---|
2509 | //pSessionThread->fStarted = false;
|
---|
2510 | //pSessionThread->fStopped = false;
|
---|
2511 | pSessionThread->hKeyPipe = NIL_RTPIPE;
|
---|
2512 | pSessionThread->Thread = NIL_RTTHREAD;
|
---|
2513 | pSessionThread->hProcess = NIL_RTPROCESS;
|
---|
2514 |
|
---|
2515 | /* Duplicate startup info. */
|
---|
2516 | pSessionThread->pStartupInfo = VbglR3GuestCtrlSessionStartupInfoDup(pSessionStartupInfo);
|
---|
2517 | AssertPtrReturn(pSessionThread->pStartupInfo, VERR_NO_MEMORY);
|
---|
2518 |
|
---|
2519 | /* Generate the secret key. */
|
---|
2520 | RTRandBytes(pSessionThread->abKey, sizeof(pSessionThread->abKey));
|
---|
2521 |
|
---|
2522 | rc = RTCritSectInit(&pSessionThread->CritSect);
|
---|
2523 | AssertRC(rc);
|
---|
2524 | if (RT_SUCCESS(rc))
|
---|
2525 | {
|
---|
2526 | /*
|
---|
2527 | * Give the session key to the host so it can validate the client.
|
---|
2528 | */
|
---|
2529 | if (VbglR3GuestCtrlSupportsOptimizations(g_idControlSvcClient))
|
---|
2530 | {
|
---|
2531 | for (uint32_t i = 0; i < 10; i++)
|
---|
2532 | {
|
---|
2533 | rc = VbglR3GuestCtrlSessionPrepare(g_idControlSvcClient, pSessionStartupInfo->uSessionID,
|
---|
2534 | pSessionThread->abKey, sizeof(pSessionThread->abKey));
|
---|
2535 | if (rc != VERR_OUT_OF_RESOURCES)
|
---|
2536 | break;
|
---|
2537 | RTThreadSleep(100);
|
---|
2538 | }
|
---|
2539 | }
|
---|
2540 | if (RT_SUCCESS(rc))
|
---|
2541 | {
|
---|
2542 | s_uCtrlSessionThread++;
|
---|
2543 |
|
---|
2544 | /*
|
---|
2545 | * Start the session child process.
|
---|
2546 | */
|
---|
2547 | rc = vgsvcVGSvcGstCtrlSessionThreadCreateProcess(pSessionStartupInfo, pSessionThread, s_uCtrlSessionThread);
|
---|
2548 | if (RT_SUCCESS(rc))
|
---|
2549 | {
|
---|
2550 | /*
|
---|
2551 | * Start the session thread.
|
---|
2552 | */
|
---|
2553 | rc = RTThreadCreateF(&pSessionThread->Thread, vgsvcGstCtrlSessionThread, pSessionThread /*pvUser*/, 0 /*cbStack*/,
|
---|
2554 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "gctls%RU32", s_uCtrlSessionThread);
|
---|
2555 | if (RT_SUCCESS(rc))
|
---|
2556 | {
|
---|
2557 | /* Wait for the thread to initialize. */
|
---|
2558 | rc = RTThreadUserWait(pSessionThread->Thread, RT_MS_1MIN);
|
---|
2559 | if ( RT_SUCCESS(rc)
|
---|
2560 | && !ASMAtomicReadBool(&pSessionThread->fShutdown))
|
---|
2561 | {
|
---|
2562 | VGSvcVerbose(2, "Thread for session ID=%RU32 started\n", pSessionThread->pStartupInfo->uSessionID);
|
---|
2563 |
|
---|
2564 | ASMAtomicXchgBool(&pSessionThread->fStarted, true);
|
---|
2565 |
|
---|
2566 | /* Add session to list. */
|
---|
2567 | RTListAppend(pList, &pSessionThread->Node);
|
---|
2568 | if (ppSessionThread) /* Return session if wanted. */
|
---|
2569 | *ppSessionThread = pSessionThread;
|
---|
2570 | return VINF_SUCCESS;
|
---|
2571 | }
|
---|
2572 |
|
---|
2573 | /*
|
---|
2574 | * Bail out.
|
---|
2575 | */
|
---|
2576 | VGSvcError("Thread for session ID=%RU32 failed to start, rc=%Rrc\n",
|
---|
2577 | pSessionThread->pStartupInfo->uSessionID, rc);
|
---|
2578 | if (RT_SUCCESS_NP(rc))
|
---|
2579 | rc = VERR_CANT_CREATE; /** @todo Find a better rc. */
|
---|
2580 | }
|
---|
2581 | else
|
---|
2582 | VGSvcError("Creating session thread failed, rc=%Rrc\n", rc);
|
---|
2583 |
|
---|
2584 | RTProcTerminate(pSessionThread->hProcess);
|
---|
2585 | uint32_t cMsWait = 1;
|
---|
2586 | while ( RTProcWait(pSessionThread->hProcess, RTPROCWAIT_FLAGS_NOBLOCK, NULL) == VERR_PROCESS_RUNNING
|
---|
2587 | && cMsWait <= 9) /* 1023 ms */
|
---|
2588 | {
|
---|
2589 | RTThreadSleep(cMsWait);
|
---|
2590 | cMsWait <<= 1;
|
---|
2591 | }
|
---|
2592 | }
|
---|
2593 |
|
---|
2594 | if (VbglR3GuestCtrlSupportsOptimizations(g_idControlSvcClient))
|
---|
2595 | VbglR3GuestCtrlSessionCancelPrepared(g_idControlSvcClient, pSessionStartupInfo->uSessionID);
|
---|
2596 | }
|
---|
2597 | else
|
---|
2598 | VGSvcVerbose(3, "VbglR3GuestCtrlSessionPrepare failed: %Rrc\n", rc);
|
---|
2599 | RTPipeClose(pSessionThread->hKeyPipe);
|
---|
2600 | pSessionThread->hKeyPipe = NIL_RTPIPE;
|
---|
2601 | RTCritSectDelete(&pSessionThread->CritSect);
|
---|
2602 | }
|
---|
2603 | RTMemFree(pSessionThread);
|
---|
2604 | }
|
---|
2605 | else
|
---|
2606 | rc = VERR_NO_MEMORY;
|
---|
2607 |
|
---|
2608 | VGSvcVerbose(3, "Spawning session thread returned returned rc=%Rrc\n", rc);
|
---|
2609 | return rc;
|
---|
2610 | }
|
---|
2611 |
|
---|
2612 |
|
---|
2613 | /**
|
---|
2614 | * Waits for a formerly opened guest session process to close.
|
---|
2615 | *
|
---|
2616 | * @return VBox status code.
|
---|
2617 | * @param pThread Guest session thread to wait for.
|
---|
2618 | * @param uTimeoutMS Waiting timeout (in ms).
|
---|
2619 | * @param fFlags Closing flags.
|
---|
2620 | */
|
---|
2621 | int VGSvcGstCtrlSessionThreadWait(PVBOXSERVICECTRLSESSIONTHREAD pThread, uint32_t uTimeoutMS, uint32_t fFlags)
|
---|
2622 | {
|
---|
2623 | RT_NOREF(fFlags);
|
---|
2624 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
2625 | /** @todo Validate closing flags. */
|
---|
2626 |
|
---|
2627 | AssertMsgReturn(pThread->Thread != NIL_RTTHREAD,
|
---|
2628 | ("Guest session thread of session %p does not exist when it should\n", pThread),
|
---|
2629 | VERR_NOT_FOUND);
|
---|
2630 |
|
---|
2631 | int rc = VINF_SUCCESS;
|
---|
2632 |
|
---|
2633 | /*
|
---|
2634 | * The spawned session process should have received the same closing request,
|
---|
2635 | * so just wait for the process to close.
|
---|
2636 | */
|
---|
2637 | if (ASMAtomicReadBool(&pThread->fStarted))
|
---|
2638 | {
|
---|
2639 | /* Ask the thread to shutdown. */
|
---|
2640 | ASMAtomicXchgBool(&pThread->fShutdown, true);
|
---|
2641 |
|
---|
2642 | VGSvcVerbose(3, "Waiting for session thread ID=%RU32 to close (%RU32ms) ...\n",
|
---|
2643 | pThread->pStartupInfo->uSessionID, uTimeoutMS);
|
---|
2644 |
|
---|
2645 | int rcThread;
|
---|
2646 | rc = RTThreadWait(pThread->Thread, uTimeoutMS, &rcThread);
|
---|
2647 | if (RT_SUCCESS(rc))
|
---|
2648 | {
|
---|
2649 | AssertMsg(pThread->fStopped, ("Thread of session ID=%RU32 not in stopped state when it should\n",
|
---|
2650 | pThread->pStartupInfo->uSessionID));
|
---|
2651 |
|
---|
2652 | VGSvcVerbose(3, "Session thread ID=%RU32 ended with rc=%Rrc\n", pThread->pStartupInfo->uSessionID, rcThread);
|
---|
2653 | }
|
---|
2654 | else
|
---|
2655 | VGSvcError("Waiting for session thread ID=%RU32 to close failed with rc=%Rrc\n", pThread->pStartupInfo->uSessionID, rc);
|
---|
2656 | }
|
---|
2657 | else
|
---|
2658 | VGSvcVerbose(3, "Thread for session ID=%RU32 not in started state, skipping wait\n", pThread->pStartupInfo->uSessionID);
|
---|
2659 |
|
---|
2660 | LogFlowFuncLeaveRC(rc);
|
---|
2661 | return rc;
|
---|
2662 | }
|
---|
2663 |
|
---|
2664 | /**
|
---|
2665 | * Waits for the specified session thread to end and remove
|
---|
2666 | * it from the session thread list.
|
---|
2667 | *
|
---|
2668 | * @return VBox status code.
|
---|
2669 | * @param pThread Session thread to destroy.
|
---|
2670 | * @param fFlags Closing flags.
|
---|
2671 | */
|
---|
2672 | int VGSvcGstCtrlSessionThreadDestroy(PVBOXSERVICECTRLSESSIONTHREAD pThread, uint32_t fFlags)
|
---|
2673 | {
|
---|
2674 | AssertPtrReturn(pThread, VERR_INVALID_POINTER);
|
---|
2675 | AssertPtrReturn(pThread->pStartupInfo, VERR_WRONG_ORDER);
|
---|
2676 |
|
---|
2677 | const uint32_t uSessionID = pThread->pStartupInfo->uSessionID;
|
---|
2678 |
|
---|
2679 | VGSvcVerbose(3, "Destroying session ID=%RU32 ...\n", uSessionID);
|
---|
2680 |
|
---|
2681 | int rc = VGSvcGstCtrlSessionThreadWait(pThread, 5 * 60 * 1000 /* 5 minutes timeout */, fFlags);
|
---|
2682 | if (RT_SUCCESS(rc))
|
---|
2683 | {
|
---|
2684 | VbglR3GuestCtrlSessionStartupInfoFree(pThread->pStartupInfo);
|
---|
2685 | pThread->pStartupInfo = NULL;
|
---|
2686 |
|
---|
2687 | /* Remove session from list and destroy object. */
|
---|
2688 | RTListNodeRemove(&pThread->Node);
|
---|
2689 |
|
---|
2690 | RTMemFree(pThread);
|
---|
2691 | pThread = NULL;
|
---|
2692 | }
|
---|
2693 |
|
---|
2694 | VGSvcVerbose(3, "Destroyed session ID=%RU32 with %Rrc\n", uSessionID, rc);
|
---|
2695 | return rc;
|
---|
2696 | }
|
---|
2697 |
|
---|
2698 | /**
|
---|
2699 | * Close all open guest session threads.
|
---|
2700 | *
|
---|
2701 | * @note Caller is responsible for locking!
|
---|
2702 | *
|
---|
2703 | * @return VBox status code.
|
---|
2704 | * @param pList Which list to close the session threads for.
|
---|
2705 | * @param fFlags Closing flags.
|
---|
2706 | */
|
---|
2707 | int VGSvcGstCtrlSessionThreadDestroyAll(PRTLISTANCHOR pList, uint32_t fFlags)
|
---|
2708 | {
|
---|
2709 | AssertPtrReturn(pList, VERR_INVALID_POINTER);
|
---|
2710 |
|
---|
2711 | int rc = VINF_SUCCESS;
|
---|
2712 |
|
---|
2713 | /*int rc = VbglR3GuestCtrlClose
|
---|
2714 | if (RT_FAILURE(rc))
|
---|
2715 | VGSvcError("Cancelling pending waits failed; rc=%Rrc\n", rc);*/
|
---|
2716 |
|
---|
2717 | PVBOXSERVICECTRLSESSIONTHREAD pSessIt;
|
---|
2718 | PVBOXSERVICECTRLSESSIONTHREAD pSessItNext;
|
---|
2719 | RTListForEachSafe(pList, pSessIt, pSessItNext, VBOXSERVICECTRLSESSIONTHREAD, Node)
|
---|
2720 | {
|
---|
2721 | int rc2 = VGSvcGstCtrlSessionThreadDestroy(pSessIt, fFlags);
|
---|
2722 | if (RT_FAILURE(rc2))
|
---|
2723 | {
|
---|
2724 | VGSvcError("Closing session thread '%s' failed with rc=%Rrc\n", RTThreadGetName(pSessIt->Thread), rc2);
|
---|
2725 | if (RT_SUCCESS(rc))
|
---|
2726 | rc = rc2;
|
---|
2727 | /* Keep going. */
|
---|
2728 | }
|
---|
2729 | }
|
---|
2730 |
|
---|
2731 | VGSvcVerbose(4, "Destroying guest session threads ended with %Rrc\n", rc);
|
---|
2732 | return rc;
|
---|
2733 | }
|
---|
2734 |
|
---|
2735 |
|
---|
2736 | /**
|
---|
2737 | * Main function for the session process.
|
---|
2738 | *
|
---|
2739 | * @returns exit code.
|
---|
2740 | * @param argc Argument count.
|
---|
2741 | * @param argv Argument vector (UTF-8).
|
---|
2742 | */
|
---|
2743 | RTEXITCODE VGSvcGstCtrlSessionSpawnInit(int argc, char **argv)
|
---|
2744 | {
|
---|
2745 | static const RTGETOPTDEF s_aOptions[] =
|
---|
2746 | {
|
---|
2747 | { "--domain", VBOXSERVICESESSIONOPT_DOMAIN, RTGETOPT_REQ_STRING },
|
---|
2748 | #ifdef DEBUG
|
---|
2749 | { "--dump-stdout", VBOXSERVICESESSIONOPT_DUMP_STDOUT, RTGETOPT_REQ_NOTHING },
|
---|
2750 | { "--dump-stderr", VBOXSERVICESESSIONOPT_DUMP_STDERR, RTGETOPT_REQ_NOTHING },
|
---|
2751 | #endif
|
---|
2752 | { "--logfile", VBOXSERVICESESSIONOPT_LOG_FILE, RTGETOPT_REQ_STRING },
|
---|
2753 | { "--user", VBOXSERVICESESSIONOPT_USERNAME, RTGETOPT_REQ_STRING },
|
---|
2754 | { "--session-id", VBOXSERVICESESSIONOPT_SESSION_ID, RTGETOPT_REQ_UINT32 },
|
---|
2755 | { "--session-proto", VBOXSERVICESESSIONOPT_SESSION_PROTO, RTGETOPT_REQ_UINT32 },
|
---|
2756 | #ifdef DEBUG
|
---|
2757 | { "--thread-id", VBOXSERVICESESSIONOPT_THREAD_ID, RTGETOPT_REQ_UINT32 },
|
---|
2758 | #endif /* DEBUG */
|
---|
2759 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING }
|
---|
2760 | };
|
---|
2761 |
|
---|
2762 | RTGETOPTSTATE GetState;
|
---|
2763 | RTGetOptInit(&GetState, argc, argv,
|
---|
2764 | s_aOptions, RT_ELEMENTS(s_aOptions),
|
---|
2765 | 1 /*iFirst*/, RTGETOPTINIT_FLAGS_OPTS_FIRST);
|
---|
2766 |
|
---|
2767 | uint32_t fSession = VBOXSERVICECTRLSESSION_FLAG_SPAWN;
|
---|
2768 |
|
---|
2769 | /* Protocol and session ID must be specified explicitly. */
|
---|
2770 | g_Session.StartupInfo.uProtocol = UINT32_MAX;
|
---|
2771 | g_Session.StartupInfo.uSessionID = UINT32_MAX;
|
---|
2772 |
|
---|
2773 | int ch;
|
---|
2774 | RTGETOPTUNION ValueUnion;
|
---|
2775 | while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
|
---|
2776 | {
|
---|
2777 | /* For options that require an argument, ValueUnion has received the value. */
|
---|
2778 | switch (ch)
|
---|
2779 | {
|
---|
2780 | case VBOXSERVICESESSIONOPT_DOMAIN:
|
---|
2781 | /* Information not needed right now, skip. */
|
---|
2782 | break;
|
---|
2783 | #ifdef DEBUG
|
---|
2784 | case VBOXSERVICESESSIONOPT_DUMP_STDOUT:
|
---|
2785 | fSession |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT;
|
---|
2786 | break;
|
---|
2787 |
|
---|
2788 | case VBOXSERVICESESSIONOPT_DUMP_STDERR:
|
---|
2789 | fSession |= VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR;
|
---|
2790 | break;
|
---|
2791 | #endif
|
---|
2792 | case VBOXSERVICESESSIONOPT_SESSION_ID:
|
---|
2793 | g_Session.StartupInfo.uSessionID = ValueUnion.u32;
|
---|
2794 | break;
|
---|
2795 |
|
---|
2796 | case VBOXSERVICESESSIONOPT_SESSION_PROTO:
|
---|
2797 | g_Session.StartupInfo.uProtocol = ValueUnion.u32;
|
---|
2798 | break;
|
---|
2799 | #ifdef DEBUG
|
---|
2800 | case VBOXSERVICESESSIONOPT_THREAD_ID:
|
---|
2801 | /* Not handled. Mainly for processs listing. */
|
---|
2802 | break;
|
---|
2803 | #endif
|
---|
2804 | case VBOXSERVICESESSIONOPT_LOG_FILE:
|
---|
2805 | {
|
---|
2806 | int rc = RTStrCopy(g_szLogFile, sizeof(g_szLogFile), ValueUnion.psz);
|
---|
2807 | if (RT_FAILURE(rc))
|
---|
2808 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Error copying log file name: %Rrc", rc);
|
---|
2809 | break;
|
---|
2810 | }
|
---|
2811 |
|
---|
2812 | case VBOXSERVICESESSIONOPT_USERNAME:
|
---|
2813 | /* Information not needed right now, skip. */
|
---|
2814 | break;
|
---|
2815 |
|
---|
2816 | /** @todo Implement help? */
|
---|
2817 |
|
---|
2818 | case 'v':
|
---|
2819 | g_cVerbosity++;
|
---|
2820 | break;
|
---|
2821 |
|
---|
2822 | case VINF_GETOPT_NOT_OPTION:
|
---|
2823 | {
|
---|
2824 | if (!RTStrICmp(ValueUnion.psz, VBOXSERVICECTRLSESSION_GETOPT_PREFIX))
|
---|
2825 | break;
|
---|
2826 | /* else fall through and bail out. */
|
---|
2827 | RT_FALL_THROUGH();
|
---|
2828 | }
|
---|
2829 | default:
|
---|
2830 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown argument '%s'", ValueUnion.psz);
|
---|
2831 | }
|
---|
2832 | }
|
---|
2833 |
|
---|
2834 | /* Check that we've got all the required options. */
|
---|
2835 | if (g_Session.StartupInfo.uProtocol == UINT32_MAX)
|
---|
2836 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No protocol version specified");
|
---|
2837 |
|
---|
2838 | if (g_Session.StartupInfo.uSessionID == UINT32_MAX)
|
---|
2839 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No session ID specified");
|
---|
2840 |
|
---|
2841 | /* Init the session object. */
|
---|
2842 | int rc = VGSvcGstCtrlSessionInit(&g_Session, fSession);
|
---|
2843 | if (RT_FAILURE(rc))
|
---|
2844 | return RTMsgErrorExit(RTEXITCODE_INIT, "Failed to initialize session object, rc=%Rrc\n", rc);
|
---|
2845 |
|
---|
2846 | rc = VGSvcLogCreate(g_szLogFile[0] ? g_szLogFile : NULL);
|
---|
2847 | if (RT_FAILURE(rc))
|
---|
2848 | return RTMsgErrorExit(RTEXITCODE_INIT, "Failed to create log file '%s', rc=%Rrc\n",
|
---|
2849 | g_szLogFile[0] ? g_szLogFile : "<None>", rc);
|
---|
2850 |
|
---|
2851 | RTEXITCODE rcExit = vgsvcGstCtrlSessionSpawnWorker(&g_Session);
|
---|
2852 |
|
---|
2853 | VGSvcLogDestroy();
|
---|
2854 | return rcExit;
|
---|
2855 | }
|
---|
2856 |
|
---|