VBoxServiceControlProcess.cpp revision 4171ffb38eb8720b2ae9a8d13e95103ab26cfd12
/* $Id$ */
/** @file
* VBoxServiceControlThread - Guest process handling.
*/
/*
* Copyright (C) 2012-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 <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
#include "VBoxServiceControl.h"
using namespace guestControl;
/*******************************************************************************
* Internal Functions *
*******************************************************************************/
static int gstcntlProcessRequest(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, PFNRT pfnFunction, unsigned cArgs, ...);
static int gstcntlProcessSetupPipe(const char *pszHowTo, int fd, PRTHANDLE ph, PRTHANDLE *pph, PRTPIPE phPipe);
/* Request handlers. */
static DECLCALLBACK(int) gstcntlProcessOnInput(PVBOXSERVICECTRLPROCESS pThis, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, bool fPendingClose, void *pvBuf, uint32_t cbBuf);
static DECLCALLBACK(int) gstcntlProcessOnOutput(PVBOXSERVICECTRLPROCESS pThis, const PVBGLR3GUESTCTRLCMDCTX pHostCtx, uint32_t uHandle, uint32_t cbToRead, uint32_t uFlags);
/**
* Initialies the passed in thread data structure with the parameters given.
*
* @return IPRT status code.
* @param pProcess Process to initialize.
* @param pSession Guest session the process is bound to.
* @param pStartupInfo Startup information.
* @param u32ContextID The context ID bound to this request / command.
*/
const PVBOXSERVICECTRLSESSION pSession,
{
/* General stuff. */
/*
* Use the initial context ID we got for starting
* the process to report back its status with the
* same context ID.
*/
/*
* Note: pProcess->ClientID will be assigned when thread is started;
* every guest process has its own client ID to detect crashes on
* a per-guest-process level.
*/
if (RT_FAILURE(rc))
return rc;
/* Copy over startup info. */
/* Adjust timeout value. */
return rc;
}
/**
* Frees a guest process. On success, pProcess will be
* free'd and thus won't be available anymore.
*
* @return IPRT status code.
* @param pProcess Guest process to free.
*/
{
/*
* Destroy other thread data.
*/
/*
* Remove from list.
*/
/*
* Destroy thread structure as final step.
*/
return VINF_SUCCESS;
}
/**
* Signals a guest process thread that we want it to shut down in
* a gentle way.
*
* @return IPRT status code.
* @param pProcess Process to stop.
*/
{
/* Do *not* set pThread->fShutdown or other stuff here!
* The guest thread loop will clean up itself. */
return GstCntlProcessHandleTerm(pProcess);
}
/**
* Releases a previously acquired guest process (decreases the refcount).
*
* @param pProcess Process to unlock.
*/
{
bool fShutdown = false;
if (RT_SUCCESS(rc))
{
}
if (fShutdown)
}
/**
* Wait for a guest process thread to shut down.
*
* @return IPRT status code.
* @param pProcess Process to wait shutting down for.
* @param RTMSINTERVAL Timeout in ms to wait for shutdown.
* @param pRc Where to store the thread's return code. Optional.
*/
{
/* pRc is optional. */
if (RT_SUCCESS(rc))
{
("Tried to wait on guest process=%p (PID %RU32) which has not been started yet\n",
/* Guest process already has been stopped, no need to wait. */
{
/* Unlock process before waiting. */
/* Do the actual waiting. */
int rcThread;
if (RT_FAILURE(rc))
{
VBoxServiceError("[PID %RU32]: Waiting for shutting down thread returned error rc=%Rrc\n",
}
else
{
if (pRc)
}
}
else
{
}
}
return rc;
}
/**
* Closes the stdin pipe of a guest process.
*
* @return IPRT status code.
* @param hPollSet The polling set.
* @param phStdInW The standard input pipe handle.
*/
{
if (rc != VERR_POLL_HANDLE_ID_NOT_FOUND)
if (*phStdInW != NIL_RTPIPE)
{
*phStdInW = NIL_RTPIPE;
}
return rc;
}
{
switch (idPollHnd)
{
return "unknown";
return "stdin";
return "stdin_writable";
return "stdout";
return "stderr";
return "ipc_notify";
default:
break;
}
return "unknown";
}
/**
* Handle an error event on standard input.
*
* @return IPRT status code.
* @param pProcess Process to handle pollset for.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phStdInW The standard input pipe handle.
*/
{
}
/**
* Handle pending output data or error on standard out or standard error.
*
* @returns IPRT status code from client send.
* @param pProcess Process to handle pollset for.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phPipeR The pipe handle.
* @param idPollHnd The pipe ID to handle.
*
*/
{
if (!phPipeR)
return VINF_SUCCESS;
#ifdef DEBUG
#endif
/* Remove pipe from poll set. */
bool fClosePipe = true; /* By default close the pipe. */
/* Check if there's remaining data to read from the pipe. */
if (*phPipeR != NIL_RTPIPE)
{
if ( RT_SUCCESS(rc2)
&& cbReadable)
{
#ifdef DEBUG
#endif
/* Veto closing the pipe yet because there's still stuff to read
* from the pipe. This can happen on UNIX-y systems where on
fClosePipe = false;
}
}
#ifdef DEBUG
else
#endif
if ( *phPipeR != NIL_RTPIPE
&& fClosePipe)
{
*phPipeR = NIL_RTPIPE;
}
return VINF_SUCCESS;
}
/**
* Handle pending output data or error on standard out or standard error.
*
* @returns IPRT status code from client send.
* @param pProcess Process to handle pollset for.
* @param fPollEvt The event mask returned by RTPollNoResume.
* @param phPipeR The pipe handle.
* @param idPollHnd The pipe ID to handle.
*
*/
{
#ifdef DEBUG
#endif
if (!phPipeR)
return VINF_SUCCESS;
int rc = VINF_SUCCESS;
#ifdef DEBUG
if (*phPipeR != NIL_RTPIPE)
{
if ( RT_SUCCESS(rc)
&& cbReadable)
{
}
}
#endif
#if 0
/* Push output to the host. */
if (fPollEvt & RTPOLL_EVT_READ)
{
/* Make sure we go another poll round in case there was too much data
for the buffer to hold. */
}
#endif
if (fPollEvt & RTPOLL_EVT_ERROR)
return rc;
}
/**
* Execution loop which runs in a dedicated per-started-process thread and
*
* @return IPRT status code.
* @param pProcess The guest process to handle.
*/
{
int rc;
int rc2;
bool fProcessAlive = true;
bool fProcessTimedOut = false;
? 100 /* Need to poll for input. */
: 1000; /* Need only poll for process exit and aborts. */
RTMSINTERVAL cMsPollCur = 0;
/*
* Assign PID to thread data.
* Also check if there already was a thread with the same PID and shut it down -- otherwise
* the first (stale) entry will be found and we get really weird results!
*/
if (RT_FAILURE(rc))
{
VBoxServiceError("Unable to assign PID=%u, to new thread, rc=%Rrc\n",
return rc;
}
/*
* Before entering the loop, tell the host that we've started the guest
* and that it's now OK to send input to the process.
*/
if (RT_FAILURE(rc))
VBoxServiceError("[PID %RU32]: Error reporting starting status to host, rc=%Rrc\n",
/*
* Process input, output, the test pipe and client requests.
*/
while ( RT_SUCCESS(rc)
{
/*
*/
continue;
cMsPollCur = 0; /* No rest until we've checked everything. */
if (RT_SUCCESS(rc2))
{
switch (idPollHnd)
{
&pProcess->hPipeStdInW);
break;
break;
break;
#ifdef DEBUG_andy
#endif
if (RT_SUCCESS(rc2))
{
/* Drain the notification pipe. */
if (RT_FAILURE(rc2))
/* Process all pending requests. */
0 /* Only process all pending requests, don't wait for new ones */);
if ( RT_FAILURE(rc2)
&& rc2 != VERR_TIMEOUT)
#ifdef DEBUG
#endif
}
break;
default:
break;
}
break; /* Abort command, or client dead or something. */
}
#if 0
VBoxServiceVerbose(4, "[PID %RU32]: Polling done, pollRc=%Rrc, pollCnt=%RU32, idPollHnd=%s, rc=%Rrc, fProcessAlive=%RTbool, fShutdown=%RTbool\n",
pProcess->uPID, rc2, RTPollSetGetCount(hPollSet), gstcntlProcessPollHandleToString(idPollHnd), rc, fProcessAlive, pProcess->fShutdown);
#endif
break; /* We were asked to shutdown. */
/*
* Check for process death.
*/
if (fProcessAlive)
{
#if 0
#endif
if (RT_SUCCESS_NP(rc2))
{
fProcessAlive = false;
/* Note: Don't bail out here yet. First check in the next block below
* if all needed pipe outputs have been consumed. */
}
else
{
continue;
{
fProcessAlive = false;
AssertFailed();
}
else
}
}
/*
* If the process has terminated and all output has been consumed,
* we should be heading out.
*/
if (!fProcessAlive)
{
if ( fProcessTimedOut
)
{
break;
}
}
/*
* Check for timed out, killing the process.
*/
{
{
fProcessTimedOut = true;
if ( MsProcessKilled == UINT64_MAX
{
break; /* Give up after 20 mins. */
continue;
}
cMilliesLeft = 10000;
}
else
}
/* Reset the polling interval since we've done all pending work. */
: RT_MS_1MIN;
if (cMilliesLeft < cMsPollCur)
}
VBoxServiceVerbose(3, "[PID %RU32]: Loop ended: rc=%Rrc, fShutdown=%RTbool, fProcessAlive=%RTbool, fProcessTimedOut=%RTbool, MsProcessKilled=%RU64\n",
pProcess->uPID, rc, pProcess->fShutdown, fProcessAlive, fProcessTimedOut, MsProcessKilled, MsProcessKilled);
/* Signal that this thread is in progress of shutting down. */
/*
* Try killing the process if it's still alive at this point.
*/
if (fProcessAlive)
{
if (MsProcessKilled == UINT64_MAX)
{
if (rc2 == VERR_NOT_FOUND)
{
fProcessAlive = false;
}
else if (RT_FAILURE(rc2))
VBoxServiceError("PID %RU32]: Killing process failed with rc=%Rrc\n",
RTThreadSleep(500);
}
for (int i = 0; i < 10 && fProcessAlive; i++)
{
if (RT_SUCCESS(rc2))
{
fProcessAlive = false;
break;
}
if (i >= 5)
{
if ( RT_FAILURE(rc)
&& rc2 != VERR_NOT_FOUND)
VBoxServiceError("PID %RU32]: Killing process failed with rc=%Rrc\n",
}
}
if (fProcessAlive)
}
/*
* Shutdown procedure:
* - Set the pProcess->fShutdown indicator to let others know we're
* not accepting any new requests anymore.
* - After setting the indicator, try to process all outstanding
* requests to make sure they're getting delivered.
*
* Note: After removing the process from the session's list it's not
* even possible for the session anymore to control what's
* happening to this thread, so be careful and don't mess it up.
*/
if (RT_SUCCESS(rc2))
{
/* Process all pending requests (but don't wait for new ones). */
if ( RT_FAILURE(rc2)
&& rc2 != VERR_TIMEOUT)
VBoxServiceError("[PID %RU32]: Processing outstanding requests failed with with rc=%Rrc\n",
}
/*
* If we don't have a client problem (RT_FAILURE(rc)) we'll reply to the
* clients exec packet now.
*/
if (RT_SUCCESS(rc))
{
{
}
{
}
{
}
else if (fProcessAlive)
{
VBoxServiceError("[PID %RU32]: Is alive when it should not!\n",
}
else if (MsProcessKilled != UINT64_MAX)
{
VBoxServiceError("[PID %RU32]: Has been killed when it should not!\n",
}
{
}
{
}
{
/* ProcessStatus.iStatus will be undefined. */
}
else
if ( RT_FAILURE(rc2)
&& rc2 == VERR_NOT_FOUND)
VBoxServiceError("[PID %RU32]: Error reporting final status to host; rc=%Rrc\n",
}
return rc;
}
/**
* Initializes a pipe's handle and pipe object.
*
* @return IPRT status code.
* @param ph The pipe's handle to initialize.
* @param phPipe The pipe's object to initialize.
*/
{
*phPipe = NIL_RTPIPE;
return VINF_SUCCESS;
}
/**
* Sets up the redirection / pipe / nothing for one of the standard handles.
*
* @returns IPRT status code. No client replies made.
* @param pszHowTo How to set up this standard handle.
* @param fd Which standard handle it is (0 == stdin, 1 ==
* stdout, 2 == stderr).
* @param ph The generic handle that @a pph may be set
* pointing to. Always set.
* @param pph Pointer to the RTProcCreateExec argument.
* Always set.
* @param phPipe Where to return the end of the pipe that we
* should service.
*/
{
int rc;
*phPipe = NIL_RTPIPE;
{
/*
* to represent the "other" end to phPipe.
*/
if (fd == 0) /* stdin? */
{
/* Connect a wrtie pipe specified by phPipe to stdin. */
}
else /* stdout or stderr? */
{
/* Connect a read pipe specified by phPipe to stdout or stderr. */
}
if (RT_FAILURE(rc))
return rc;
}
{
/*
*/
if (RT_FAILURE(rc))
return rc;
}
else /* Add other piping stuff here. */
return rc;
}
/**
* Expands a file name / path to its real content. This only works on Windows
* for now (e.g. translating "%TEMP%\foo.exe" to "C:\Windows\Temp" when starting
* with system / administrative rights).
*
* @return IPRT status code.
* @param pszPath Path to resolve.
* @param pszExpanded Pointer to string to store the resolved path in.
* @param cbExpanded Size (in bytes) of string to store the resolved path.
*/
{
int rc = VINF_SUCCESS;
#ifdef RT_OS_WINDOWS
#else
/* No expansion for non-Windows yet. */
#endif
#ifdef DEBUG
#endif
return rc;
}
/**
* Resolves the full path of a specified executable name. This function also
* resolves internal VBoxService tools to its appropriate executable path + name if
* VBOXSERVICE_NAME is specified as pszFileName.
*
* @return IPRT status code.
* @param pszFileName File name to resolve.
* @param pszResolved Pointer to a string where the resolved file name will be stored.
* @param cbResolved Size (in bytes) of resolved file name string.
*/
static int gstcntlProcessResolveExecutable(const char *pszFileName,
{
int rc = VINF_SUCCESS;
char szPathToResolve[RTPATH_MAX];
{
/* Resolve executable name of this process. */
}
else
{
/* Take the raw argument to resolve. */
}
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
}
if (RT_FAILURE(rc))
VBoxServiceError("Failed to lookup executable \"%s\" with rc=%Rrc\n",
pszFileName, rc);
return rc;
}
/**
* Constructs the argv command line by resolving environment variables
* and relative paths.
*
* @return IPRT status code.
* @param pszArgv0 First argument (argv0), either original or modified version. Optional.
* @param papszArgs Original argv command line from the host, starting at argv[1].
* @param ppapszArgv Pointer to a pointer with the new argv command line.
* Needs to be freed with RTGetOptArgvFree.
*/
static int gstcntlProcessAllocateArgv(const char *pszArgv0,
const char * const *papszArgs,
bool fExpandArgs, char ***ppapszArgv)
{
VBoxServiceVerbose(3, "GstCntlProcessPrepareArgv: pszArgv0=%p, papszArgs=%p, fExpandArgs=%RTbool, ppapszArgv=%p\n",
int rc = VINF_SUCCESS;
{
return VERR_BUFFER_OVERFLOW;
}
/* Allocate new argv vector (adding + 2 for argv0 + termination). */
if (!papszNewArgv)
return VERR_NO_MEMORY;
#ifdef DEBUG
#endif
size_t i = 0; /* Keep the argument counter in scope for cleaning up on failure. */
if (RT_SUCCESS(rc))
{
for (; i < cArgs; i++)
{
char *pszArg;
#if 0 /* Arguments expansion -- untested. */
if (fExpandArgs)
{
/* According to MSDN the limit on older Windows version is 32K, whereas
* Vista+ there are no limits anymore. We still stick to 4K. */
char szExpanded[_4K];
# ifdef RT_OS_WINDOWS
# else
/* No expansion for non-Windows yet. */
# endif
if (RT_SUCCESS(rc))
}
else
#endif
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
/* Terminate array. */
}
}
if (RT_FAILURE(rc))
{
for (i; i > 0; i--)
RTStrFree(papszNewArgv[i]);
}
return rc;
}
/**
* Assigns a valid PID to a guest control thread and also checks if there already was
* another (stale) guest process which was using that PID before and destroys it.
*
* @return IPRT status code.
* @param pProcess Process to assign PID to.
* @param uPID PID to assign to the specified guest control execution thread.
*/
{
if (RT_SUCCESS(rc))
{
/* Search old threads using the desired PID and shut them down completely -- it's
* not used anymore. */
bool fTryAgain;
do
{
fTryAgain = false;
{
{
uPID += 391939;
fTryAgain = true;
break;
}
}
} while (fTryAgain);
/* Assign PID to current thread. */
}
return rc;
}
void gstcntlProcessFreeArgv(char **papszArgv)
{
if (papszArgv)
{
size_t i = 0;
while (papszArgv[i])
}
}
/**
*
* @return IPRT status code.
* @param pszExec Full qualified path of process to start (without arguments).
* @param papszArgs Pointer to array of command line arguments.
* @param hEnv Handle to environment block to use.
* @param fFlags Process execution flags.
* @param phStdIn Handle for the process' stdin pipe.
* @param phStdOut Handle for the process' stdout pipe.
* @param phStdErr Handle for the process' stderr pipe.
* @param pszAsUser User name (account) to start the process under.
* @param pszPassword Password of the specified user.
* @param phProcess Pointer which will receive the process handle after
* successful process start.
*/
static int gstcntlProcessCreateProcess(const char *pszExec, const char * const *papszArgs, RTENV hEnv, uint32_t fFlags,
{
int rc = VINF_SUCCESS;
char szExecExp[RTPATH_MAX];
/* Do we need to expand environment variables in arguments? */
#ifdef RT_OS_WINDOWS
/*
* If sysprep should be executed do this in the context of VBoxService, which
* (usually, if started by SCM) has administrator rights. Because of that a UI
* won't be shown (doesn't have a desktop).
*/
{
/* Use a predefined sysprep path as default. */
/** @todo Check digital signature of file above before executing it? */
/*
* On Windows Vista (and up) sysprep is located in "system32\\Sysprep\\sysprep.exe",
* so detect the OS and use a different path.
*/
if ( fRet
{
#ifndef RT_ARCH_AMD64
/* Don't execute 64-bit sysprep from a 32-bit service host! */
char szSysWow64[RTPATH_MAX];
{
}
if ( RT_SUCCESS(rc)
&& RTPathExists(szSysWow64))
VBoxServiceVerbose(0, "Warning: This service is 32-bit; could not execute sysprep on 64-bit OS!\n");
#endif
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_FAILURE(rc))
}
else if (!fRet)
if (RT_SUCCESS(rc))
{
char **papszArgsExp;
if (RT_SUCCESS(rc))
{
/* As we don't specify credentials for the sysprep process, it will
* run under behalf of the account VBoxService was started under, most
* likely local system. */
}
}
if (RT_FAILURE(rc))
return rc;
}
#endif /* RT_OS_WINDOWS */
#ifdef VBOXSERVICE_TOOLBOX
{
/* We want to use the internal toolbox (all internal
* tools are starting with "vbox_" (e.g. "vbox_cat"). */
}
else
{
#endif
/*
* Do the environment variables expansion on executable and arguments.
*/
#ifdef VBOXSERVICE_TOOLBOX
}
#endif
if (RT_SUCCESS(rc))
{
char **papszArgsExp;
papszArgs /* Append the rest of the argument vector (if any). */,
if (RT_FAILURE(rc))
{
/* Don't print any arguments -- may contain passwords or other sensible data! */
}
else
{
uint32_t uProcFlags = 0;
if (fFlags)
{
if (fFlags & EXECUTEPROCESSFLAG_HIDDEN)
}
/* If no user name specified run with current credentials (e.g.
*
* Otherwise use the RTPROC_FLAGS_SERVICE to use some special authentication
* code (at least on Windows) for running processes as different users
* started from our system service. */
#ifdef DEBUG
for (size_t i = 0; papszArgsExp[i]; i++)
#endif
/* Do normal execution. */
}
}
return rc;
}
#ifdef DEBUG
{
if (!cbBuf)
return VINF_SUCCESS;
char szFile[RTPATH_MAX];
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
if (RT_SUCCESS(rc))
{
}
}
return rc;
}
#endif
/**
* The actual worker routine (loop) for a started guest process.
*
* @return IPRT status code.
* @param PVBOXSERVICECTRLPROCESS Guest process.
*/
{
if (RT_FAILURE(rc))
{
VBoxServiceError("Process thread \"%s\" (%p) failed to connect to the guest control service, rc=%Rrc\n",
return rc;
}
/* The process thread is not interested in receiving any commands;
* tell the host service. */
0 /* Filter mask to add */, 0 /* Filter mask to remove */);
if (RT_FAILURE(rc))
{
/* Non-critical. */
}
if (RT_FAILURE(rc))
{
VBoxServiceError("Errorwhile adding guest process \"%s\" (%p) to session process list, rc=%Rrc\n",
return rc;
}
bool fSignalled = false; /* Indicator whether we signalled the thread user event already. */
/*
* Prepare argument list.
*/
char **papszArgs;
/* Did we get the same result? */
/*
* Prepare environment variables list.
*/
if (RT_SUCCESS(rc))
{
/* Prepare environment list. */
{
uint32_t i = 0;
{
/* sanity check */
{
break;
}
if (cbStr < 0)
{
break;
}
}
}
}
/*
* Create the environment.
*/
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
size_t i;
for (i = 0; i < uNumEnvVars && papszEnv; i++)
{
if (RT_FAILURE(rc))
break;
}
if (RT_SUCCESS(rc))
{
/*
* Setup the redirection of the standard stuff.
*/
/** @todo consider supporting: gcc stuff.c >file 2>&1. */
if (RT_SUCCESS(rc))
{
1 /*STDOUT_FILENO*/,
if (RT_SUCCESS(rc))
{
2 /*STDERR_FILENO*/,
if (RT_SUCCESS(rc))
{
/*
* Create a poll set for the pipes and let the
* transport layer add stuff to it as well.
*/
if (RT_SUCCESS(rc))
{
#if 0
/* Add reading event to pollset to get some more information. */
#endif
/* Stdin. */
if (RT_SUCCESS(rc))
/* Stdout. */
if (RT_SUCCESS(rc))
/* Stderr. */
if (RT_SUCCESS(rc))
/* IPC notification pipe. */
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
if (RT_SUCCESS(rc))
{
rc = gstcntlProcessCreateProcess(pProcess->StartupInfo.szCmd, papszArgs, hEnv, pProcess->StartupInfo.uFlags,
if (RT_FAILURE(rc))
/*
* Tell the session thread that it can continue
* spawning guest processes. This needs to be done after the new
* process has been started because otherwise signal handling
* on (Open) Solaris does not work correctly (see @bugref{5068}).
*/
if (RT_SUCCESS(rc))
fSignalled = true;
if (RT_SUCCESS(rc))
{
/*
* Close the child ends of any pipes and redirected files.
*/
/* Enter the process main loop. */
/*
* The handles that are no longer in the set have
* been closed by the above call in order to prevent
* the guest from getting stuck accessing them.
* So, NIL the handles to avoid closing them again.
*/
{
}
}
}
}
if (phStdErr)
}
if (phStdOut)
}
}
}
}
{
if (RT_FAILURE(rc))
{
if ( RT_FAILURE(rc2)
&& rc2 != VERR_NOT_FOUND)
VBoxServiceError("[PID %RU32]: Could not report process failure error; rc=%Rrc (process error %Rrc)\n",
}
/* Disconnect this client from the guest control service. This also cancels all
* outstanding host requests. */
}
/* Free argument + environment variable lists. */
if (uNumEnvVars)
{
for (uint32_t i = 0; i < uNumEnvVars; i++)
}
if (uNumArgs)
/*
* If something went wrong signal the user event so that others don't wait
* forever on this thread.
*/
/* Finally, update stopped status. */
return rc;
}
{
return rc;
}
/**
* Thread main routine for a started process.
*
* @return IPRT status code.
* @param RTTHREAD Pointer to the thread's data.
* @param void* User-supplied argument pointer.
*
*/
{
return gstcntlProcessProcessWorker(pProcess);
}
{
return rc;
}
/**
* Executes (starts) a process on the guest. This causes a new thread to be created
* so that this function will not block the overall program execution.
*
* @return IPRT status code.
* @param pSession Guest session.
* @param pStartupInfo Startup info.
* @param uContextID Context ID to associate the process to start with.
*/
{
/*
* Allocate new thread data and assign it to our thread list.
*/
PVBOXSERVICECTRLPROCESS pProcess = (PVBOXSERVICECTRLPROCESS)RTMemAlloc(sizeof(VBOXSERVICECTRLPROCESS));
if (!pProcess)
return VERR_NO_MEMORY;
if (RT_SUCCESS(rc))
{
static uint32_t s_uCtrlExecThread = 0;
if (s_uCtrlExecThread++ == UINT32_MAX)
s_uCtrlExecThread = 0; /* Wrap around to not let IPRT freak out. */
if (RT_FAILURE(rc))
{
VBoxServiceError("Creating thread for guest process \"%s\" failed: rc=%Rrc, pProcess=%p\n",
}
else
{
/* Wait for the thread to initialize. */
|| RT_FAILURE(rc))
{
VBoxServiceError("Thread for process \"%s\" failed to start, rc=%Rrc\n",
}
else
{
}
}
}
return rc;
}
const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
{
int rc;
{
{
}
else
}
else
/*
* If this is the last write + we have really have written all data
* we need to close the stdin pipe on our end and remove it from
* the poll set.
*/
if ( fPendingClose
{
if (RT_SUCCESS(rc))
}
if (RT_SUCCESS(rc))
{
}
else
{
if (rc == VERR_BAD_PIPE)
else if (rc == VERR_BUFFER_OVERFLOW)
/* else undefined */
}
/*
* If there was an error and we did not set the host status
* yet, then do it now.
*/
if ( RT_FAILURE(rc)
&& uStatus == INPUT_STS_UNDEFINED)
{
}
#ifdef DEBUG
#endif
if (RT_SUCCESS(rc))
#ifdef DEBUG
#endif
return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
}
const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
{
int rc;
if (pvBuf)
{
? &pThis->hPipeStdOutR
: &pThis->hPipeStdErrR;
if (*phPipe != NIL_RTPIPE)
{
if (RT_FAILURE(rc))
{
*phPipe = NIL_RTPIPE;
if (rc == VERR_BROKEN_PIPE)
}
}
else
#ifdef DEBUG
if (RT_SUCCESS(rc))
{
&& ( uHandle == OUTPUT_HANDLE_ID_STDOUT
)
{
char szDumpFile[RTPATH_MAX];
if (RT_SUCCESS(rc))
}
&& uHandle == OUTPUT_HANDLE_ID_STDERR)
{
char szDumpFile[RTPATH_MAX];
if (RT_SUCCESS(rc))
}
}
#endif
if (RT_SUCCESS(rc))
{
#ifdef DEBUG
VBoxServiceVerbose(3, "[PID %RU32]: Read %RU32 bytes output: uHandle=%RU32, CID=%RU32, uFlags=%x\n",
#endif
* data which the host needs to work with -- so just pass through all data unfiltered! */
/* Note: Since the context ID is unique the request *has* to be completed here,
* regardless whether we got data or not! Otherwise the waiting events
* on the host never will get completed! */
if ( RT_FAILURE(rc)
rc = VINF_SUCCESS;
}
}
else
rc = VERR_NO_MEMORY;
#ifdef DEBUG
#endif
return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
}
{
{
}
return VINF_SUCCESS; /** @todo Return rc here as soon as RTReqQueue todos are fixed. */
}
int gstcntlProcessRequestExV(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
{
/* pHostCtx is optional. */
if (!fAsync)
if (RT_SUCCESS(rc))
{
#ifdef DEBUG
VBoxServiceVerbose(3, "[PID %RU32]: gstcntlProcessRequestExV fAsync=%RTbool, uTimeoutMS=%RU32, cArgs=%u\n",
#endif
if (fAsync)
{
Assert(uTimeoutMS == 0);
}
if (RT_SUCCESS(rc))
{
/* Wake up the process' notification pipe to get
* the request being processed. */
if ( RT_SUCCESS(rc)
&& cbWritten != 1)
{
VBoxServiceError("[PID %RU32]: Notification pipe got %zu bytes instead of 1\n",
}
VBoxServiceError("[PID %RU32]: Writing to notification pipe failed, rc=%Rrc\n",
}
else
VBoxServiceError("[PID %RU32]: RTReqQueueCallV failed, rc=%Rrc\n",
if (RT_SUCCESS(rc))
}
#ifdef DEBUG
#endif
return rc;
}
int gstcntlProcessRequestAsync(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
{
/* pHostCtx is optional. */
return rc;
}
int gstcntlProcessRequestWait(PVBOXSERVICECTRLPROCESS pProcess, const PVBGLR3GUESTCTRLCMDCTX pHostCtx,
{
/* pHostCtx is optional. */
return rc;
}
{
}
{
}
{
return gstcntlProcessOnTerm(pProcess);
}