VBoxServiceControlSession.cpp revision 9bff17fe6983cfda2ddd98f1979841bcb48e78e7
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * VBoxServiceControlSession - Guest session handling. Also handles
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * the forked session processes.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Copyright (C) 2013 Oracle Corporation
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * available from http://www.virtualbox.org. This file is free software;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * you can redistribute it and/or modify it under the terms of the GNU
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * General Public License (GPL) as published by the Free Software
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/*******************************************************************************
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync* Header Files *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync*******************************************************************************/
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncusing namespace guestControl;
4260648b018894247c6a73e244050be76c28f857vboxsync/*******************************************************************************
4260648b018894247c6a73e244050be76c28f857vboxsync* Externals *
4260648b018894247c6a73e244050be76c28f857vboxsync*******************************************************************************/
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncextern int VBoxServiceLogCreate(const char *pszLogFile);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncextern void VBoxServiceLogDestroy(void);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/*******************************************************************************
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync* Internal Functions *
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync*******************************************************************************/
248c89033c87fed7229aa29bbbc4f4698fb13687vboxsyncstatic int gstcntlSessionFileDestroy(PVBOXSERVICECTRLFILE pFile);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic PVBOXSERVICECTRLFILE gstcntlSessionGetFile(const PVBOXSERVICECTRLSESSION pSession, uint32_t uHandle);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionGetOutput(const PVBOXSERVICECTRLSESSION pSession, uint32_t uPID, uint32_t uCID, uint32_t uHandleId, uint32_t cMsTimeout, void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileOpen(PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileClose(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileRead(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileWrite(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx, void *pvScratchBuf, size_t cbScratchBuf);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileSeek(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileTell(const PVBOXSERVICECTRLSESSION pSession, PVBGLR3GUESTCTRLHOSTCTX pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionSetInput(const PVBOXSERVICECTRLSESSION pSession, uint32_t uPID, uint32_t uCID, bool fPendingClose, void *pvBuf, uint32_t cbBuf, uint32_t *pcbWritten);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic DECLCALLBACK(int) gstcntlSessionThread(RTTHREAD ThreadSelf, void *pvUser);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync/** Generic option indices for session fork arguments. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncstatic int gstcntlSessionFileDestroy(PVBOXSERVICECTRLFILE pFile)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Remove file entry in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Destroy this object. */
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic PVBOXSERVICECTRLFILE gstcntlSessionGetFile(const PVBOXSERVICECTRLSESSION pSession,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /** @todo Use a map later! */
4260648b018894247c6a73e244050be76c28f857vboxsync RTListForEach(&pSession->lstFiles, pFileCur, VBOXSERVICECTRLFILE, Node)
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionDumpToFile(const char *pszFileName, void *pvBuf, size_t cbBuf)
4260648b018894247c6a73e244050be76c28f857vboxsync AssertPtrReturn(pszFileName, VERR_INVALID_POINTER);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = RTPathAppend(szFile, sizeof(szFile), pszFileName);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(4, "Dumping %ld bytes to \"%s\"\n", cbBuf, szFile);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = RTFileOpen(&fh, szFile, RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = RTFileWrite(fh, pvBuf, cbBuf, NULL /* pcbWritten */);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileOpen(PVBOXSERVICECTRLSESSION pSession,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* File to open. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Open mode. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Disposition. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Creation mode. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Offset. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync PVBOXSERVICECTRLFILE pFile = (PVBOXSERVICECTRLFILE)RTMemAlloc(sizeof(VBOXSERVICECTRLFILE));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (!RTStrPrintf(pFile->szName, sizeof(pFile->szName), "%s", szFile))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint64_t fFlags = RTFILE_O_OPEN_CREATE | RTFILE_O_WRITE | RTFILE_O_DENY_WRITE; /** @todo Modes! */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTFileOpen(&pFile->hFile, pFile->szName, fFlags);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Seeking is optional. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync int rc2 = RTFileSeek(pFile->hFile, (int64_t)uOffset, RTFILE_SEEK_BEGIN, NULL /* Current offset */);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "[File %s]: Seeking to offset %RU64 failed; rc=%Rrc\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "[File %s]: Opening failed; rc=%Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsync uHandle = VBOX_GUESTCTRL_CONTEXTID_GET_OBJECT(pHostCtx->uContextID);
4260648b018894247c6a73e244050be76c28f857vboxsync /* rc = */ RTListAppend(&pSession->lstFiles, &pFile->Node);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "[File %s]: Opened (ID=%RU32)\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_OPEN cplOpen = { rc, uHandle };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_OPEN, &cplOpen, sizeof(cplOpen));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("[File %s]: Failed to report file open status, rc=%Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileClose(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetClose(pHostCtx, &uHandle /* File handle to close */);
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_CLOSE cplClose = { rc };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_CLOSE, &cplClose, sizeof(cplClose));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file close status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileRead(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetRead(pHostCtx, &uHandle, &cbToRead);
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTFileRead(pFile->hFile, pvDataRead, cbToRead, &cbRead);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
a247bf7db58d0a80be3122656e22a606b6071e13vboxsync CALLBACKPAYLOAD_FILE_NOTFIY_READ cplRead = { rc, (uint32_t)cbRead, pvDataRead };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_READ, &cplRead, sizeof(cplRead));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file read status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileReadAt(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTFileReadAt(pFile->hFile, iOffset, pvDataRead, cbToRead, &cbRead);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
a247bf7db58d0a80be3122656e22a606b6071e13vboxsync CALLBACKPAYLOAD_FILE_NOTFIY_READ cplRead = { rc, (uint32_t)cbRead, pvDataRead };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_READ, &cplRead, sizeof(cplRead));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file read status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileWrite(const PVBOXSERVICECTRLSESSION pSession,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetWrite(pHostCtx, &uHandle,
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTFileWrite(pFile->hFile, pvScratchBuf, cbScratchBuf, &cbWritten);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_WRITE cplWrite = { rc, (uint32_t)cbWritten };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_WRITE, &cplWrite, sizeof(cplWrite));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file write status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileWriteAt(const PVBOXSERVICECTRLSESSION pSession,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetWriteAt(pHostCtx, &uHandle,
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_WRITE cplWrite = { rc, (uint32_t)cbWritten };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_WRITE, &cplWrite, sizeof(cplWrite));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file write status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileSeek(const PVBOXSERVICECTRLSESSION pSession,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint64_t uOffset; /* Will be converted to int64_t. */
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetSeek(pHostCtx, &uHandle,
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_SEEK cplSeek = { rc, uOffsetActual };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_SEEK, &cplSeek, sizeof(cplSeek));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file seek status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionHandleFileTell(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlFileGetTell(pHostCtx, &uHandle);
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLFILE pFile = gstcntlSessionGetFile(pSession, uHandle);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Report back in any case. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync CALLBACKPAYLOAD_FILE_NOTFIY_TELL cplTell = { rc, uOffsetActual };
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlFileNotify(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync GUEST_FILE_NOTIFYTYPE_TELL, &cplTell, sizeof(cplTell));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Failed to report file tell status, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsync * Handles starting a guest processes.
4260648b018894247c6a73e244050be76c28f857vboxsync * @returns IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pSession Guest session.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pHostCtx Host context.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandleProcExec(PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if ( (pHostCtx->uProtocol < 2 && pHostCtx->uNumParms == 11)
4260648b018894247c6a73e244050be76c28f857vboxsync || (pHostCtx->uProtocol >= 2 && pHostCtx->uNumParms == 12)
4260648b018894247c6a73e244050be76c28f857vboxsync /* Initialize maximum environment block size -- needed as input
4260648b018894247c6a73e244050be76c28f857vboxsync * parameter to retrieve the stuff from the host. On output this then
4260648b018894247c6a73e244050be76c28f857vboxsync * will contain the actual block size. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Command */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Flags */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Arguments */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.szArgs, sizeof(startupInfo.szArgs), &startupInfo.uNumArgs,
4260648b018894247c6a73e244050be76c28f857vboxsync /* Environment */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.szEnv, &startupInfo.cbEnv, &startupInfo.uNumEnvVars,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Credentials; for hosts with VBox < 4.3 (protocol version 1).
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * For protocl v2 and up the credentials are part of the session
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * opening call. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.szPassword, sizeof(startupInfo.szPassword),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Timeout (in ms) */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Process priority */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Process affinity */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.uAffinity, sizeof(startupInfo.uAffinity), &startupInfo.uNumAffinity);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Request to start process szCmd=%s, uFlags=0x%x, szArgs=%s, szEnv=%s, szUser=%s, szPassword=%s, uTimeout=%RU32\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.uNumArgs ? startupInfo.szArgs : "<None>",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync startupInfo.uNumEnvVars ? startupInfo.szEnv : "<None>",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Reaping stopped guest processes failed with rc=%Rrc\n", rc);
4260648b018894247c6a73e244050be76c28f857vboxsync /* Keep going. */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionProcessStartAllowed(pSession, &fStartAllowed);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = GstCntlProcessStart(pSession, &startupInfo, pHostCtx->uContextID);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = VERR_NOT_SUPPORTED; /* Unsupported number of parameters. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* In case of an error we need to notify the host to not wait forever for our response. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Starting process failed with rc=%Rrc, protocol=%RU32, parameters=%RU32\n",
4260648b018894247c6a73e244050be76c28f857vboxsync * Note: The context ID can be 0 because we mabye weren't able to fetch the command
4260648b018894247c6a73e244050be76c28f857vboxsync * from the host. The host in case has to deal with that!
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlProcCbStatus(pHostCtx->uClientID, pHostCtx->uContextID,
4260648b018894247c6a73e244050be76c28f857vboxsync 0 /* PID, invalid */,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Error sending start process status to host, rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsync * Handles input for a started process by copying the received data into its
4260648b018894247c6a73e244050be76c28f857vboxsync * stdin pipe.
4260648b018894247c6a73e244050be76c28f857vboxsync * @returns IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pvScratchBuf The scratch buffer.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param cbScratchBuf The scratch buffer size for retrieving the input data.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandleProcInput(PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync AssertPtrReturn(cbScratchBuf, VERR_INVALID_PARAMETER);
4260648b018894247c6a73e244050be76c28f857vboxsync AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
4260648b018894247c6a73e244050be76c28f857vboxsync uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
4260648b018894247c6a73e244050be76c28f857vboxsync uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
4260648b018894247c6a73e244050be76c28f857vboxsync * Ask the host for the input data.
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlProcGetInput(pHostCtx, &uPID, &uFlags,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("[PID %RU32]: Failed to retrieve exec input command! Error: %Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("[PID %RU32]: Too much input received! cbSize=%u, cbScratchBuf=%u\n",
4260648b018894247c6a73e244050be76c28f857vboxsync * Is this the last input block we need to deliver? Then let the pipe know ...
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(4, "[PID %RU32]: Got last input block of size %u ...\n",
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(4, "[PID %RU32]: Written input, CID=%u, rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
4260648b018894247c6a73e244050be76c28f857vboxsync uPID, pHostCtx->uContextID, rc, uFlags, fPendingClose, cbSize, cbWritten);
4260648b018894247c6a73e244050be76c28f857vboxsync * If there was an error and we did not set the host status
4260648b018894247c6a73e244050be76c28f857vboxsync * yet, then do it now.
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "[PID %RU32]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
4260648b018894247c6a73e244050be76c28f857vboxsync uPID, pHostCtx->uContextID, uStatus, uFlags, cbWritten);
4260648b018894247c6a73e244050be76c28f857vboxsync /* Note: Since the context ID is unique the request *has* to be completed here,
4260648b018894247c6a73e244050be76c28f857vboxsync * regardless whether we got data or not! Otherwise the progress object
4260648b018894247c6a73e244050be76c28f857vboxsync * on the host never will get completed! */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = VbglR3GuestCtrlProcCbStatusInput(pHostCtx->uClientID, pHostCtx->uContextID, uPID,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("[PID %RU32]: Failed to report input status! Error: %Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsync * Handles the guest control output command.
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandleProcOutput(PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlProcGetOutput(pHostCtx, &uPID, &uHandleID, &uFlags);
4260648b018894247c6a73e244050be76c28f857vboxsync pHostCtx->uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "[PID %RU32]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
4260648b018894247c6a73e244050be76c28f857vboxsync uPID, rc, pHostCtx->uContextID, cbRead, uHandleID, uFlags);
4260648b018894247c6a73e244050be76c28f857vboxsync if ( (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDOUT)
4260648b018894247c6a73e244050be76c28f857vboxsync if (!RTStrPrintf(szDumpFile, sizeof(szDumpFile), "VBoxService_Session%RU32_PID%RU32_StdOut.txt",
4260648b018894247c6a73e244050be76c28f857vboxsync rc = gstcntlSessionDumpToFile(szDumpFile, pBuf, cbRead);
4260648b018894247c6a73e244050be76c28f857vboxsync else if ( (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_DUMPSTDERR)
4260648b018894247c6a73e244050be76c28f857vboxsync || uHandleID == OUTPUT_HANDLE_ID_STDOUT_DEPRECATED))
4260648b018894247c6a73e244050be76c28f857vboxsync if (!RTStrPrintf(szDumpFile, sizeof(szDumpFile), "VBoxService_Session%RU32_PID%RU32_StdErr.txt",
4260648b018894247c6a73e244050be76c28f857vboxsync rc = gstcntlSessionDumpToFile(szDumpFile, pBuf, cbRead);
4260648b018894247c6a73e244050be76c28f857vboxsync /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
4260648b018894247c6a73e244050be76c28f857vboxsync * data which the host needs to work with -- so just pass through all data unfiltered! */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Note: Since the context ID is unique the request *has* to be completed here,
4260648b018894247c6a73e244050be76c28f857vboxsync * regardless whether we got data or not! Otherwise the progress object
4260648b018894247c6a73e244050be76c28f857vboxsync * on the host never will get completed! */
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = VbglR3GuestCtrlProcCbOutput(pHostCtx->uClientID, pHostCtx->uContextID, uPID, uHandleID, uFlags,
4260648b018894247c6a73e244050be76c28f857vboxsync else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("[PID %RU32]: Error handling output command! Error: %Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandleProcTerminate(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlProcGetTerminate(pHostCtx, &uPID);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlProcessRequestAllocEx(&pRequest, VBOXSERVICECTRLREQUEST_PROC_TERM,
4260648b018894247c6a73e244050be76c28f857vboxsync NULL /* pvBuf */, 0 /* cbBuf */, pHostCtx->uContextID);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pProcess = GstCntlSessionAcquireProcess(pSession, uPID);
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandleProcWaitFor(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = VbglR3GuestCtrlProcGetWaitFor(pHostCtx, &uPID, &uWaitFlags, &uTimeoutMS);
4260648b018894247c6a73e244050be76c28f857vboxsync VBOXSERVICECTRLREQDATA_WAIT_FOR reqData = { uWaitFlags, uTimeoutMS };
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlProcessRequestAllocEx(&pRequest, VBOXSERVICECTRLREQUEST_WAIT_FOR,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pProcess = GstCntlSessionAcquireProcess(pSession, uPID);
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionHandler(PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync volatile bool *pfShutdown)
4260648b018894247c6a73e244050be76c28f857vboxsync AssertPtrReturn(pvScratchBuf, VERR_INVALID_POINTER);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Only anonymous sessions (that is, sessions which run with local
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * service privileges) or forked session processes can do certain
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * operations. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync bool fImpersonated = ( pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_FORK
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync || pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_ANONYMOUS);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(1, "We were asked to quit ...\n");
4260648b018894247c6a73e244050be76c28f857vboxsync /* Fall thru is intentional. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Shutdown this fork. */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandleProcExec(pSession, pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandleProcInput(pSession, pHostCtx,
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandleProcOutput(pSession, pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandleProcTerminate(pSession, pHostCtx);
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandleProcWaitFor(pSession, pHostCtx);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ? gstcntlSessionHandleFileClose(pSession, pHostCtx)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ? gstcntlSessionHandleFileReadAt(pSession, pHostCtx,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ? gstcntlSessionHandleFileWrite(pSession, pHostCtx,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ? gstcntlSessionHandleFileWriteAt(pSession, pHostCtx,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Unsupported message from host, uMsg=%RU32, cParms=%RU32\n",
4260648b018894247c6a73e244050be76c28f857vboxsync /* Don't terminate here; just wait for the next message. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Thread main routine for a forked guest session. This
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * thread runs in the main executable to control the forked
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * session process.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @return IPRT status code.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param RTTHREAD Pointer to the thread's data.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param void* User-supplied argument pointer.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsyncstatic DECLCALLBACK(int) gstcntlSessionThread(RTTHREAD ThreadSelf, void *pvUser)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLSESSIONTHREAD pThread = (PVBOXSERVICECTRLSESSIONTHREAD)pvUser;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint32_t uSessionID = pThread->StartupInfo.uSessionID;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(3, "Session ID=%RU32 thread running, client ID=%RU32\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Error connecting to guest control service, rc=%Rrc\n", rc);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Let caller know that we're done initializing. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint32_t uTimeoutsMS = 30 * 1000; /** @todo Make this configurable. Later. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rcWait = RTProcWaitNoResume(pThread->hProcess, RTPROCWAIT_FLAGS_NOBLOCK,
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ("Got unexpected rc=%Rrc while waiting for session process termination\n", rcWait));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(3, "Guest session ID=%RU32 thread was asked to terminate, waiting for session process to exit ...\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync if (RTTimeMilliTS() - u64TimeoutStart > uTimeoutsMS)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(3, "Guest session ID=%RU32 process did not shut down within time\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(2, "Guest session ID=%RU32 process terminated with rc=%Rrc, reason=%ld, status=%d\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint32_t uSessionStatus = GUEST_SESSION_NOTIFYTYPE_UNDEFINED;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint32_t uSessionRc = VINF_SUCCESS; /** uint32_t vs. int. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync for (int i = 0; i < 3; i++)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(2, "Guest session ID=%RU32 process still alive, killing attempt %d/3\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(2, "Guest session ID=%RU32 process has been killed with rc=%Rc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ? GUEST_SESSION_NOTIFYTYPE_TOK : GUEST_SESSION_NOTIFYTYPE_TOA;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync AssertMsgFailed(("Unhandled process termination reason (%ld)",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(3, "Guest session ID=%RU32 thread ended with sessionStatus=%ld, sessionRc=%Rrc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Report final status. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync Assert(uSessionStatus != GUEST_SESSION_NOTIFYTYPE_UNDEFINED);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Reporting session ID=%RU32 final status failed with rc=%Rrc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(3, "Session ID=%RU32 thread ended with rc=%Rrc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncRTEXITCODE gstcntlSessionForkWorker(PVBOXSERVICECTRLSESSION pSession)
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VBoxServiceVerbose(0, "Hi, this is guest session ID=%RU32\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Set session filter. */
4260648b018894247c6a73e244050be76c28f857vboxsync VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(pSession->StartupInfo.uSessionID);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VbglR3GuestCtrlMsgSetFilter(uClientID, uFilterAdd, 0 /* Filter remove */);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VBoxServiceVerbose(3, "Setting message filterAdd=%RU32 returned %Rrc\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* No session filter available. Skip. */
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(1, "Using client ID=%RU32\n", uClientID);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Error connecting to guest control service, rc=%Rrc\n", rc);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Report started status. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBOX_GUESTCTRL_CONTEXTID_MAKE_SESSION(pSession->StartupInfo.uSessionID),
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Reporting session ID=%RU32 started status failed with rc=%Rrc\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Allocate a scratch buffer for commands which also send
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * payload data with them. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync uint32_t cbScratchBuf = _64K; /** @todo Make buffer size configurable via guest properties/argv! */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertReturn(RT_IS_POWER_OF_TWO(cbScratchBuf), RTEXITCODE_FAILURE);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync bool fShutdown = false;
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "Waiting for host msg ...\n");
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VbglR3GuestCtrlMsgWaitFor(uClientID, &uMsg, &cParms);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(4, "Message requires %RU32 parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "Msg=%RU32 (%RU32 parms) retrieved\n", uMsg, cParms);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Set number of parameters for current host context. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* ... and pass it on to the session handler. */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlSessionHandler(pSession, uMsg, &ctxHost,
4260648b018894247c6a73e244050be76c28f857vboxsync /* Let's sleep for a bit and let others run ... */
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(0, "Session %RU32 ended\n", pSession->StartupInfo.uSessionID);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Disconnecting client ID=%RU32 ...\n", uClientID);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Session worker returned with rc=%Rrc\n", rc);
4260648b018894247c6a73e244050be76c28f857vboxsync return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
4260648b018894247c6a73e244050be76c28f857vboxsync * Finds a (formerly) started guest process given by its PID and locks it.
4260648b018894247c6a73e244050be76c28f857vboxsync * Must be unlocked by the caller with GstCntlProcessRelease().
4260648b018894247c6a73e244050be76c28f857vboxsync * @return PVBOXSERVICECTRLTHREAD Locked guest process if found, otherwise NULL.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param PVBOXSERVICECTRLSESSION Pointer to guest session where to search process in.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uPID PID to search for.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncPVBOXSERVICECTRLPROCESS GstCntlSessionAcquireProcess(PVBOXSERVICECTRLSESSION pSession, uint32_t uPID)
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = RTCritSectEnter(&pSession->csControlThreads);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync RTListForEach(&pSession->lstControlThreadsActive, pThreadCur, VBOXSERVICECTRLPROCESS, Node)
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = RTCritSectLeave(&pSession->csControlThreads);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncint GstCntlSessionClose(PVBOXSERVICECTRLSESSION pSession)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceVerbose(0, "Session %RU32 is about to close ...\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Close all guest processes.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Signal all guest processes in the active list that we want to shutdown. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync RTListForEach(&pSession->lstControlThreadsActive, pProcess, VBOXSERVICECTRLPROCESS, Node)
4260648b018894247c6a73e244050be76c28f857vboxsync /* Wait for all active threads to shutdown and destroy the active thread list. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pProcess = RTListGetFirst(&pSession->lstControlThreadsActive, VBOXSERVICECTRLPROCESS, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pNext = RTListNodeGetNext(&pProcess->Node, VBOXSERVICECTRLPROCESS, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync bool fLast = RTListNodeIsLast(&pSession->lstControlThreadsActive, &pProcess->Node);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Guest process thread failed to stop; rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsync /* Keep going. */
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Reaping inactive threads failed with rc=%Rrc\n", rc);
4260648b018894247c6a73e244050be76c28f857vboxsync AssertMsg(RTListIsEmpty(&pSession->lstControlThreadsActive),
4260648b018894247c6a73e244050be76c28f857vboxsync ("Guest process active thread list still contains entries when it should not\n"));
4260648b018894247c6a73e244050be76c28f857vboxsync AssertMsg(RTListIsEmpty(&pSession->lstControlThreadsInactive),
4260648b018894247c6a73e244050be76c28f857vboxsync ("Guest process inactive thread list still contains entries when it should not\n"));
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * Close all left guest files.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync pFile = RTListGetFirst(&pSession->lstFiles, VBOXSERVICECTRLFILE, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLFILE pNext = RTListNodeGetNext(&pFile->Node, VBOXSERVICECTRLFILE, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync bool fLast = RTListNodeIsLast(&pSession->lstFiles, &pFile->Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Unable to close file \"%s\"; rc=%Rrc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Keep going. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync ("Guest file list still contains entries when it should not\n"));
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionDestroy(PVBOXSERVICECTRLSESSION pSession)
4260648b018894247c6a73e244050be76c28f857vboxsync /* Destroy critical section. */
4260648b018894247c6a73e244050be76c28f857vboxsync * Gets output from stdout/stderr of a specified guest process.
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pSession Guest session.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uPID PID of process to retrieve the output from.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uCID Context ID.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uHandleId Stream ID (stdout = 0, stderr = 2) to get the output from.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param cMsTimeout Timeout (in ms) to wait for output becoming
4260648b018894247c6a73e244050be76c28f857vboxsync * available.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pvBuf Pointer to a pre-allocated buffer to store the output.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param cbBuf Size (in bytes) of the pre-allocated buffer.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pcbRead Pointer to number of bytes read. Optional.
4260648b018894247c6a73e244050be76c28f857vboxsyncstatic int gstcntlSessionGetOutput(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync AssertPtrNullReturn(pcbRead, VERR_INVALID_POINTER);
4260648b018894247c6a73e244050be76c28f857vboxsync VBOXSERVICECTRLREQUESTTYPE reqType = VBOXSERVICECTRLREQUEST_UNKNOWN; /* (gcc maybe, well, wrong.) */
4260648b018894247c6a73e244050be76c28f857vboxsync rc = GstCntlProcessRequestAllocEx(&pRequest, reqType, pvBuf, cbBuf, uCID);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pProcess = GstCntlSessionAcquireProcess(pSession, uPID);
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionInit(PVBOXSERVICECTRLSESSION pSession, uint32_t uFlags)
4260648b018894247c6a73e244050be76c28f857vboxsync if (pSession->uFlags & VBOXSERVICECTRLSESSION_FLAG_FORK)
4260648b018894247c6a73e244050be76c28f857vboxsync /* Protocol must be specified explicitly. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Session ID must be specified explicitly. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Init critical section for protecting the thread lists. */
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = RTCritSectInit(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync * Sets the specified guest thread to a certain list.
4260648b018894247c6a73e244050be76c28f857vboxsync ** @todo Still needed?
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pSession Guest session.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param enmList List to move thread to.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pProcess Guest process to set.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionListSet(PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync AssertReturn(enmList > VBOXSERVICECTRLTHREADLIST_UNKNOWN, VERR_INVALID_PARAMETER);
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = RTCritSectEnter(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Setting thread (PID %RU32) to list %d\n",
4260648b018894247c6a73e244050be76c28f857vboxsync AssertMsgFailed(("Unknown list type: %u", enmList));
4260648b018894247c6a73e244050be76c28f857vboxsync /* If thread was assigned to a list before,
4260648b018894247c6a73e244050be76c28f857vboxsync * remove the thread from the old list first. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* Add thread to desired list. */
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = RTCritSectLeave(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync * Determines whether starting a new guest process according to the
4260648b018894247c6a73e244050be76c28f857vboxsync * maximum number of concurrent guest processes defined is allowed or not.
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pbAllowed True if starting (another) guest process
4260648b018894247c6a73e244050be76c28f857vboxsync * is allowed, false if not.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionProcessStartAllowed(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = RTCritSectEnter(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync * Check if we're respecting our memory policy by checking
4260648b018894247c6a73e244050be76c28f857vboxsync * how many guest processes are started and served already.
4260648b018894247c6a73e244050be76c28f857vboxsync if (pSession->uProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync RTListForEach(&pSession->lstControlThreadsActive, pThread, VBOXSERVICECTRLPROCESS, Node)
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Maximum served guest processes set to %u, running=%u\n",
4260648b018894247c6a73e244050be76c28f857vboxsync int32_t iProcsLeft = (pSession->uProcsMaxKept - uProcsRunning - 1);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Maximum running guest processes reached (%u)\n",
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = RTCritSectLeave(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync * Reaps all inactive guest process threads.
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncint GstCntlSessionReapProcesses(PVBOXSERVICECTRLSESSION pSession)
4260648b018894247c6a73e244050be76c28f857vboxsync int rc = RTCritSectEnter(&pSession->csControlThreads);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync RTListGetFirst(&pSession->lstControlThreadsInactive, VBOXSERVICECTRLPROCESS, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLPROCESS, Node);
4260648b018894247c6a73e244050be76c28f857vboxsync bool fLast = RTListNodeIsLast(&pSession->lstControlThreadsInactive, &pThread->Node);
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = GstCntlProcessWait(pThread, 30 * 1000 /* 30 seconds max. */,
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Freeing guest process thread failed with rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Waiting on guest process thread failed with rc=%Rrc\n", rc2);
4260648b018894247c6a73e244050be76c28f857vboxsync /* Keep going. */
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = RTCritSectLeave(&pSession->csControlThreads);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(4, "Reaping threads returned with rc=%Rrc\n", rc);
4260648b018894247c6a73e244050be76c28f857vboxsync * Injects input to a specified running guest process.
4260648b018894247c6a73e244050be76c28f857vboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pSession Guest session.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uPID PID of process to set the input for.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param uCID Context ID to use for reporting back.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pvBuf Pointer to a buffer containing the actual input data.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param cbBuf Size (in bytes) of the input buffer data.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pcbWritten Pointer to number of bytes written to the process. Optional.
4260648b018894247c6a73e244050be76c28f857vboxsyncint gstcntlSessionSetInput(const PVBOXSERVICECTRLSESSION pSession,
4260648b018894247c6a73e244050be76c28f857vboxsync /* pvBuf is optional. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* cbBuf is optional. */
4260648b018894247c6a73e244050be76c28f857vboxsync /* pcbWritten is optional. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync PVBOXSERVICECTRLPROCESS pProcess = GstCntlSessionAcquireProcess(pSession, uPID);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Creates a guest session. This will spawn a new VBoxService.exe instance under
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * behalf of the given user which then will act as a session host. On successful
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * open, the session will be added to the given session thread list.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pList Which list to use to store the session thread in.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @param pSessionStartupInfo Session startup info.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param ppSessionThread Returns newly created session thread on success.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Optional.
4260648b018894247c6a73e244050be76c28f857vboxsync const PVBOXSERVICECTRLSESSIONSTARTUPINFO pSessionStartupInfo,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync AssertPtrReturn(pSessionStartupInfo, VERR_INVALID_POINTER);
4260648b018894247c6a73e244050be76c28f857vboxsync /* ppSessionThread is optional. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Check for existing session in debug mode. Should never happen because of
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Main consistency. */
4260648b018894247c6a73e244050be76c28f857vboxsync RTListForEach(pList, pSessionCur, VBOXSERVICECTRLSESSIONTHREAD, Node)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (pSessionCur->StartupInfo.uSessionID == pSessionStartupInfo->uSessionID)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync AssertMsgFailed(("Guest session %RU32 (%p) already exists when it should not",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pSessionCur->StartupInfo.uSessionID, pSessionCur));
4260648b018894247c6a73e244050be76c28f857vboxsync PVBOXSERVICECTRLSESSIONTHREAD pSession = (PVBOXSERVICECTRLSESSIONTHREAD)RTMemAllocZ(sizeof(VBOXSERVICECTRLSESSIONTHREAD));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Copy over session startup info. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync memcpy(&pSession->StartupInfo, pSessionStartupInfo, sizeof(VBOXSERVICECTRLSESSIONSTARTUPINFO));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Is this an anonymous session? */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Anonymous sessions run with the same privileges as the main VBoxService executable. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync bool fAnonymous = !RT_BOOL(strlen(pSession->StartupInfo.szUser));
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VBoxServiceVerbose(3, "New anonymous guest session ID=%RU32 created, uFlags=%x, using protocol %RU32\n",
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VBoxServiceVerbose(3, "Forking new guest session ID=%RU32, szUser=%s, szPassword=%s, szDomain=%s, uFlags=%x, using protocol %RU32\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Fork child doing the actual session handling. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync char *pszExeName = RTProcGetExecutablePath(szExeName, sizeof(szExeName));
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync char szParmUserName[GUESTPROCESS_MAX_USER_LEN + 32];
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync if (!RTStrPrintf(szParmUserName, sizeof(szParmUserName), "--username=%s", pSession->StartupInfo.szUser))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (RT_SUCCESS(rc) && !RTStrPrintf(szParmSessionID, sizeof(szParmSessionID), "--session-id=%RU32",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (RT_SUCCESS(rc) && !RTStrPrintf(szParmSessionProto, sizeof(szParmSessionProto), "--session-proto=%RU32",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync int iOptIdx = 0; /* Current index in argument vector. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Add same verbose flags as parent process. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync for (int i = 0; i < g_cVerbosity && RT_SUCCESS(rc2); i++)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (i == 0)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc2 = RTStrCat(szParmVerbose, sizeof(szParmVerbose), "-");
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc2 = RTStrCat(szParmVerbose, sizeof(szParmVerbose), "v");
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Add log file handling. Each session will have an own
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * log file, naming based on the parent log file. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (!RTStrPrintf(szParmLogFile, sizeof(szParmLogFile),
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceError("Error building session logfile string for session %RU32 (user %s), rc=%Rrc\n",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pSessionStartupInfo->uSessionID, pSessionStartupInfo->szUser, rc2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(4, "\t%s\n", papszArgs[iOptIdx++]);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync | RTPROC_FLAGS_HIDDEN; /** @todo More flags from startup info? */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync#if 0 /* Pipe handling not needed (yet). */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Setup pipes. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = GstcntlProcessSetupPipe("|", 0 /*STDIN_FILENO*/,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync &pSession->StdIn.hChild, &pSession->StdIn.phChild, &pSession->hStdInW);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = GstcntlProcessSetupPipe("|", 1 /*STDOUT_FILENO*/,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync &pSession->StdOut.hChild, &pSession->StdOut.phChild, &pSession->hStdOutR);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = GstcntlProcessSetupPipe("|", 2 /*STDERR_FILENO*/,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync &pSession->StdErr.hChild, &pSession->StdErr.phChild, &pSession->hStdErrR);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdInW, RTPOLL_EVT_ERROR,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdOutR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTPollSetAddPipe(pSession->hPollSet, pSession->hStdErrR, RTPOLL_EVT_READ | RTPOLL_EVT_ERROR,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Fork the thing. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /** @todo Do we need a custom environment block? */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = RTProcCreateEx(pszExeName, papszArgs, RTENV_DEFAULT, uProcFlags,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync pSession->StdIn.phChild, pSession->StdOut.phChild, pSession->StdErr.phChild,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync !fAnonymous ? pSession->StartupInfo.szPassword : NULL,
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * Close the child ends of any pipes and redirected files.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync int rc2 = RTHandleClose(pSession->StdIn.phChild); AssertRC(rc2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc2 = RTHandleClose(pSession->StdOut.phChild); AssertRC(rc2);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc2 = RTHandleClose(pSession->StdErr.phChild); AssertRC(rc2);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = RTFileOpenBitBucket(&hStdIn.u.hFile, RTFILE_O_READ);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = RTFileOpenBitBucket(&hStdOutAndErr.u.hFile, RTFILE_O_WRITE);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /** @todo Do we need a custom/cloned environment block? */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = RTProcCreateEx(pszExeName, papszArgs, RTENV_DEFAULT, uProcFlags,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync !fAnonymous ? pSession->StartupInfo.szPassword : NULL,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Start session thread. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync s_uCtrlSessionThread = 0; /* Wrap around to not let IPRT freak out. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync rc = RTThreadCreateF(&pSession->Thread, gstcntlSessionThread,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, "sess%u", s_uCtrlSessionThread);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync VBoxServiceError("Creating session thread failed, rc=%Rrc\n", rc);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Wait for the thread to initialize. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = RTThreadUserWait(pSession->Thread, 60 * 1000 /* 60s timeout */);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Thread for session ID=%RU32 failed to start, rc=%Rrc\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = VERR_CANT_CREATE; /** @todo Find a better rc. */
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Add session to list. */
4260648b018894247c6a73e244050be76c28f857vboxsync if (ppSessionThread) /* Return session if wanted. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync VBoxServiceVerbose(3, "Forking returned returned rc=%Rrc\n", rc);
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * Closes a formerly opened guest session and removes it from
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * the session list.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @return IPRT status code.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync * @param pThread Guest session thread to close.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param uFlags Closing flags.
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsyncint GstCntlSessionThreadClose(PVBOXSERVICECTRLSESSIONTHREAD pThread, uint32_t uFlags)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /** @todo Validate closing flags. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync AssertMsgFailed(("Guest session thread of session %p does not exist when it should",
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* The fork should have received the same closing request,
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * so just wait 30s for the process to close. On timeout kill
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * it in a not so gentle manner. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Ask the thread to shutdown. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync uint32_t cMsTimeout = 30 * 1000; /** @todo 30s default. Make this configurable. Later. */
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Waiting for session thread ID=%RU32 to close (%RU32ms) ...\n",
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync rc = RTThreadWait(pThread->Thread, cMsTimeout, &rcThread);
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceError("Waiting for session thread ID=%RU32 to close failed with rc=%Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsync VBoxServiceVerbose(3, "Session thread ID=%RU32 ended with rc=%Rrc\n",
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync /* Remove session from list and destroy object. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync VBoxServiceError("Closing session ID=%RU32 failed with rc=%Rrc\n",
4260648b018894247c6a73e244050be76c28f857vboxsync * Close all formerly opened guest session threads.
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync * @return IPRT status code.
4260648b018894247c6a73e244050be76c28f857vboxsync * @param pList Which list to close the session threads for.
3fb3de312d1ff675e0f7cc62a7d46cbb1d5d9353vboxsync * @param uFlags Closing flags.
4260648b018894247c6a73e244050be76c28f857vboxsyncint GstCntlSessionThreadCloseAll(PRTLISTANCHOR pList, uint32_t uFlags)
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync = RTListGetFirst(pList, VBOXSERVICECTRLSESSIONTHREAD, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync RTListGetNext(pList, pSessionCur, VBOXSERVICECTRLSESSIONTHREAD, Node);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync bool fLast = RTListNodeIsLast(pList, &pSessionCur->Node);
4260648b018894247c6a73e244050be76c28f857vboxsync int rc2 = GstCntlSessionThreadClose(pSessionCur, uFlags);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Keep going. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsyncRTEXITCODE VBoxServiceControlSessionForkInit(int argc, char **argv)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync { "--logfile", VBOXSERVICESESSIONOPT_LOG_FILE, RTGETOPT_REQ_STRING },
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync { "--username", VBOXSERVICESESSIONOPT_USERNAME, RTGETOPT_REQ_STRING },
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync { "--session-id", VBOXSERVICESESSIONOPT_SESSION_ID, RTGETOPT_REQ_UINT32 },
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync { "--session-proto", VBOXSERVICESESSIONOPT_SESSION_PROTO, RTGETOPT_REQ_UINT32 },
4260648b018894247c6a73e244050be76c28f857vboxsync uint32_t uSessionFlags = VBOXSERVICECTRLSESSION_FLAG_FORK;
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync /* Init the session object. */
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync int rc = GstCntlSessionInit(&g_Session, uSessionFlags);
9bff17fe6983cfda2ddd98f1979841bcb48e78e7vboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize session object, rc=%Rrc\n", rc);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* For options that require an argument, ValueUnion has received the value. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync if (!RTStrPrintf(g_szLogFile, sizeof(g_szLogFile), "%s", ValueUnion.psz))
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Unable to set logfile name to '%s'",
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /** @todo. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /** @todo Implement help? */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync /* Ignore; might be "guestsession" main command. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown command '%s'", ValueUnion.psz);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync break; /* Never reached. */
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Initialization failed with rc=%Rrc", rc);
4260648b018894247c6a73e244050be76c28f857vboxsync return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No protocol version specified");
4260648b018894247c6a73e244050be76c28f857vboxsync if (g_Session.StartupInfo.uSessionID == UINT32_MAX)
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No session ID specified");
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync rc = VBoxServiceLogCreate(strlen(g_szLogFile) ? g_szLogFile : NULL);
462e60a19d02a99b2b1a5c08dff74bb0808d707cvboxsync return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to create release log (%s, %Rrc)",