VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxServiceControlSession.cpp@ 58029

Last change on this file since 58029 was 58029, checked in by vboxsync, 9 years ago

VBoxService: Using prefix 'VGSvc', code style/width cleanups. No real changes.

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

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette