VBoxServiceControlSession.cpp revision 462e60a19d02a99b2b1a5c08dff74bb0808d707c
/* $Id$ */
/** @file
* VBoxServiceControlSession - Guest session handling. Also handles
* the forked session processes.
*/
/*
* Copyright (C) 2013 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#include "VBoxServiceInternal.h"
#include "VBoxServiceUtils.h"
#include "VBoxServiceControl.h"
using namespace guestControl;
/** List of guest control sessions (VBOXSERVICECTRLSESSION). */
extern RTLISTANCHOR g_lstControlSessions;
extern int VBoxServiceLogCreate(const char *pszLogFile);
extern void VBoxServiceLogDestroy(void);
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int gstcntlSessionHandleFileWrite(uint32_t uClientId, uint32_t cParms, void *pvScratchBuf, size_t cbScratchBuf);
/** The session ID of the forked session process. */
/** The session HGCM protocol of the forked session process. */
/** List of guest control files (VBOXSERVICECTRLFILE). */
static RTLISTANCHOR g_lstSessionFiles;
/** Generic option indices for session fork arguments. */
enum
{
VBOXSERVICESESSIONOPT_LOG_FILE = 1000,
};
{
if (RT_SUCCESS(rc))
{
/* Remove file entry in any case. */
/* Destroy this object. */
}
return rc;
}
{
/** @todo Use a map later! */
{
return pFileCur;
}
return NULL;
}
{
char szFile[RTPATH_MAX];
char szOpenMode[64];
char szDisposition[64];
uint32_t uCreationMode = 0;
/* File to open. */
/* Open mode. */
szOpenMode, sizeof(szOpenMode),
/* Disposition. */
szDisposition, sizeof(szDisposition),
/* Creation mode. */
/* Offset. */
&uOffset);
if (RT_SUCCESS(rc))
{
if (pFile)
{
if (RT_SUCCESS(rc))
{
if ( RT_SUCCESS(rc)
&& uOffset)
{
/* Seeking is optional. */
if (RT_FAILURE(rc2))
}
else
}
if (RT_SUCCESS(rc))
{
}
if (RT_FAILURE(rc))
}
else
rc = VERR_NO_MEMORY;
/* Report back in any case. */
if (RT_FAILURE(rc2))
VBoxServiceError("[File %s]: Failed to report file open status, rc=%Rrc\n",
if (RT_SUCCESS(rc))
}
return rc;
}
{
if (RT_SUCCESS(rc))
{
if (pFile)
{
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
if (RT_SUCCESS(rc))
{
void *pvDataRead = pvScratchBuf;
if (pFile)
{
if (cbToRead)
{
if (cbToRead > cbScratchBuf)
{
if (!pvDataRead)
rc = VERR_NO_MEMORY;
}
}
else
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if ( cbToRead > cbScratchBuf
&& pvDataRead)
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
if (RT_SUCCESS(rc))
{
void *pvDataRead = pvScratchBuf;
if (pFile)
{
if (cbToRead)
{
if (cbToRead > cbScratchBuf)
{
if (!pvDataRead)
rc = VERR_NO_MEMORY;
}
}
else
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if ( cbToRead > cbScratchBuf
&& pvDataRead)
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
&cbToWrite);
if (RT_SUCCESS(rc))
{
if (pFile)
{
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
if (RT_SUCCESS(rc))
{
if (pFile)
{
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
uint64_t uOffsetActual = 0;
&uSeekMethod, &uOffset);
if (RT_SUCCESS(rc))
{
if (pFile)
{
unsigned uSeekMethodIPRT;
switch (uSeekMethod)
{
break;
break;
case GUEST_FILE_SEEKTYPE_END:
break;
default:
break;
}
if (RT_SUCCESS(rc))
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
uint64_t uOffsetActual = 0;
if (RT_SUCCESS(rc))
{
if (pFile)
{
}
else
rc = VERR_NOT_FOUND;
/* Report back in any case. */
if (RT_FAILURE(rc2))
if (RT_SUCCESS(rc))
}
return rc;
}
{
bool fSessionFilter = true;
if (RT_SUCCESS(rc))
{
/* Set session filter. */
if ( RT_FAILURE(rc)
&& rc == VERR_NOT_SUPPORTED)
{
/* No session filter available. Skip. */
fSessionFilter = false;
rc = VINF_SUCCESS;
}
}
/* Allocate a scratch buffer for commands which also send
* payload data with them. */
if (RT_SUCCESS(rc))
{
if (!pvScratchBuf)
rc = VERR_NO_MEMORY;
}
if (RT_SUCCESS(rc))
{
bool fShutdown = false;
for (;;)
{
if (rc == VERR_TOO_MUCH_DATA)
{
VBoxServiceVerbose(4, "Message requires %RU32 parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
}
else if (RT_FAILURE(rc))
VBoxServiceVerbose(3, "Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
if (RT_SUCCESS(rc))
{
}
/** @todo Guest session ID change detection? */
switch (uMsg)
{
/* Fall thru is intentional. */
case HOST_SESSION_CLOSE:
/* Shutdown this fork. */
fShutdown = true; /* Shutdown in any case. */
break;
case HOST_FILE_OPEN:
break;
case HOST_FILE_CLOSE:
break;
case HOST_FILE_READ:
break;
case HOST_FILE_READ_AT:
break;
case HOST_FILE_WRITE:
break;
case HOST_FILE_WRITE_AT:
break;
case HOST_FILE_SEEK:
break;
case HOST_FILE_TELL:
break;
default:
/* Don't terminate here; just wait for the next message. */
break;
}
if (fShutdown)
break;
}
}
if (pvScratchBuf)
}
/**
* Creates a guest session. This will spawn a new VBoxService.exe instance under
* behalf of the given user which then will act as a session host.
*
* @return IPRT status code.
* @param pSessionStartupInfo Session startup info.
* @param pNode Returns newly created session node on success.
* Optional.
*/
{
/* pNode is optional. */
#ifdef DEBUG
/* Check for existing session in debug mode. Should never happen because of
* Main consistency. */
{
{
AssertMsgFailed(("Guest session %RU32 (%p) already exists when it should not\n",
return VERR_ALREADY_EXISTS;
}
}
#endif
int rc = VINF_SUCCESS;
PVBOXSERVICECTRLSESSION pSession = (PVBOXSERVICECTRLSESSION)RTMemAllocZ(sizeof(VBOXSERVICECTRLSESSION));
if (pSession)
{
/* Copy over session startup info. */
VBoxServiceVerbose(3, "Forking new guest session szUser=%s, szPassword=%s, szDomain=%s, uFlags=%x, using protocol %RU32\n",
#ifdef DEBUG
#else
"XXX", /* Never show passwords in release mode. */
#endif
/* Fork child doing the actual session handling. */
char szExeName[RTPATH_MAX];
if (pszExeName)
{
if (!RTStrPrintf(szParmUserName, sizeof(szParmUserName), "--username=%s", pSession->StartupInfo.szUser))
char szParmSessionID[32];
{
}
char szParmSessionProto[32];
if (RT_SUCCESS(rc) && !RTStrPrintf(szParmSessionProto, sizeof(szParmSessionProto), "--session-proto=%RU32",
{
}
if (RT_SUCCESS(rc))
{
int iOptIdx = 0; /* Current index in argument vector. */
char const *papszArgs[8];
/* Add same verbose flags as parent process. */
int rc2 = VINF_SUCCESS;
char szParmVerbose[32] = { 0 };
{
if (i == 0)
if (RT_FAILURE(rc2))
break;
}
if (RT_SUCCESS(rc2))
/* Add log file handling. Each session will have an own
* log file, naming based on the parent log file. */
char szParmLogFile[RTPATH_MAX];
if ( RT_SUCCESS(rc2)
&& strlen(g_szLogFile))
{
if (pszLogFile)
{
if (RTPathHasExt(pszLogFile))
char *pszLogSuffix;
pSessionStartupInfo->szUser) < 0)
{
}
else
{
if (RT_SUCCESS(rc2))
{
"--logfile %s", pszLogFile))
{
}
}
}
if (RT_FAILURE(rc2))
VBoxServiceError("Error building session logfile string for session %RU32 (user %s), rc=%Rrc\n",
if (pszLogExt)
}
if (RT_SUCCESS(rc2))
}
else
if (g_cVerbosity > 3)
{
iOptIdx = 0;
}
| RTPROC_FLAGS_HIDDEN; /** @todo More flags from startup info? */
#if 0 /* Pipe handling not needed (yet). */
/* Setup pipes. */
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
/* Fork the thing. */
/** @todo Do we need a custom environment block? */
}
if (RT_SUCCESS(rc))
{
/*
* Close the child ends of any pipes and redirected files.
*/
}
}
}
#else
/** @todo Do we need a custom environment block? */
#endif
}
}
else
if (RT_SUCCESS(rc))
{
/* Add session to list. */
if (pNode) /* Return node if wanted. */
}
else
{
}
}
else
rc = VERR_NO_MEMORY;
return rc;
}
/**
* Closes a formerly opened guest session.
*
* @return IPRT status code.
* @param pSession Guest session to close.
* @param uFlags Termination flags.
*/
{
/** @todo Implement session termination. */
return VERR_NOT_IMPLEMENTED;
}
/**
* Close all formerly opened guest sessions.
*
* @return IPRT status code.
* @param uFlags Termination flags.
*/
{
int rc = VINF_SUCCESS;
{
if (RT_SUCCESS(rc))
{
/* Keep going. */
}
}
return rc;
}
{
static const RTGETOPTDEF s_aOptions[] =
{
};
int ch;
int rc = VINF_SUCCESS;
&& RT_SUCCESS(rc))
{
/* For options that require an argument, ValueUnion has received the value. */
switch (ch)
{
break;
/** @todo. */
break;
break;
break;
/** @todo Implement help? */
case 'v':
g_cVerbosity++;
break;
case VINF_GETOPT_NOT_OPTION:
/* Ignore; might be "guestsession" main command. */
break;
default:
break; /* Never reached. */
}
}
if (RT_FAILURE(rc))
if (g_uSessionID == UINT32_MAX)
if (RT_FAILURE(rc))
return rcExit;
}
{
/* Close all left guest files. */
while (pFile)
{
if (RT_FAILURE(rc2))
{
VBoxServiceError("Unable to close file \"%s\"; rc=%Rrc\n",
/* Keep going. */
}
if (fLast)
break;
}
("Guest file list still contains entries when it should not\n"));
/** @todo Add guest process termination here. Later. */
return VINF_SUCCESS; /** @todo */
}