VBoxServiceControl.cpp revision d7a909bc753ae3e2160db551a3a568e082594d9a
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * VBoxServiceControl - Host-driven Guest Control.
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2011 Oracle Corporation
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * available from http://www.virtualbox.org. This file is free software;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * you can redistribute it and/or modify it under the terms of the GNU
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * General Public License (GPL) as published by the Free Software
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncusing namespace guestControl;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
f5e53763b0a581b0299e98028c6c52192eb06785vboxsync* Global Variables *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** The control interval (milliseconds). */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** The semaphore we're blocking our main control thread on. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** The guest control service client ID. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync/** How many started guest processes are kept into memory for supplying
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * information to the host. Default is 25 processes. If 0 is specified,
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * the maximum number of processes is unlimited. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** List of guest control threads (VBOXSERVICECTRLTHREAD). */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** Critical section protecting g_GuestControlExecThreads. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Internal Functions *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @todo Shorten "VBoxServiceControl" to "gstsvcCntl". */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlStartAllowed(bool *pbAllowed);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdStartProc(uint32_t u32ClientId, uint32_t uNumParms);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdSetInput(uint32_t u32ClientId, uint32_t uNumParms, size_t cbMaxBufSize);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdGetOutput(uint32_t u32ClientId, uint32_t uNumParms);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(int) VBoxServiceControlPreInit(void)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Read the service options from the VM's guest properties.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Note that these options can be overridden by the command line options later.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(0, "Control: Guest property service is not available, skipping\n");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Failed to connect to the guest property service! Error: %Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--control-procs-max-kept",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /* Nothing to do here yet. */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync/** @copydoc VBOXSERVICE::pfnOption */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsyncstatic DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
65697a26b524640b83828b715160c798c43a0424vboxsync /* no short options */;
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync else if (!strcmp(argv[*pi], "--control-procs-max-kept"))
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync/** @copydoc VBOXSERVICE::pfnInit */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsyncstatic DECLCALLBACK(int) VBoxServiceControlInit(void)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * If not specified, find the right interval default.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * Then create the event sem to block on.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VbglR3GuestCtrlConnect(&g_GuestControlSvcClientID);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Service client ID: %#x\n", g_GuestControlSvcClientID);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Init thread list. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTCritSectInit(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* If the service was not found, we disable this service without
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync causing VBoxService to fail. */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync VBoxServiceVerbose(0, "Control: Guest control service is not available\n");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Failed to connect to the guest control service! Error: %Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** @copydoc VBOXSERVICE::pfnWorker */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Tell the control thread that it can continue
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * spawning services.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Execution loop.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync VBoxServiceVerbose(3, "Control: Waiting for host msg ...\n");
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync rc = VbglR3GuestCtrlWaitForHostMsg(g_GuestControlSvcClientID, &uMsg, &cParms);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(4, "Control: Message requires %ld parameters, but only 2 supplied -- retrying request (no error!)...\n", cParms);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VINF_SUCCESS; /* Try to get "real" message in next block below. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
65697a26b524640b83828b715160c798c43a0424vboxsync VBoxServiceVerbose(3, "Control: Msg=%u (%u parms) retrieved\n", uMsg, cParms);
65697a26b524640b83828b715160c798c43a0424vboxsync VBoxServiceVerbose(3, "Control: Host asked us to quit ...\n");
65697a26b524640b83828b715160c798c43a0424vboxsync rc = VBoxServiceControlHandleCmdStartProc(g_GuestControlSvcClientID, cParms);
65697a26b524640b83828b715160c798c43a0424vboxsync /** @todo Make buffer size configurable via guest properties/argv! */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlHandleCmdSetInput(g_GuestControlSvcClientID, cParms, _1M /* Buffer size */);
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, cParms);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Don't terminate here; just wait for the next message. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Do we need to shutdown? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Let's sleep for a bit and let others run ... */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Handles starting processes on the guest.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns IPRT status code.
ead016c68c61b5f2e1fe4d237054eebea9327d4bvboxsync * @param idClient The HGCM client session ID.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cParms The number of parameters the host is offering.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsyncstatic int VBoxServiceControlHandleCmdStartProc(uint32_t idClient, uint32_t cParms)
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync#if 0 /* for valgrind */
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync /* Command */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Flags */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Arguments */
65697a26b524640b83828b715160c798c43a0424vboxsync /* Environment */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Credentials */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Timelimit */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(3, "Control: Request to start process szCmd=%s, uFlags=0x%x, szArgs=%s, szEnv=%s, szUser=%s, uTimeout=%u\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync szCmd, uFlags, cArgs ? szArgs : "<None>", cEnvVars ? szEnv : "<None>", szUser, uTimeLimitMS);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync rc = VBoxServiceControlStartAllowed(&fStartAllowed);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /** @todo Put the following params into a struct! */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync rc = VBoxServiceControlThreadStart(idClient, uContextID,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Insert thread node into thread list. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*rc =*/ RTListAppend(&g_GuestControlThreads, pThreadNode);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_INVALID_PARAMETER; /* Incorrect number of parameters. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* In case of an error we need to notify the host to not wait forever for our response. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Starting process failed with rc=%Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc2 = VbglR3GuestCtrlExecReportStatus(idClient, uContextID, 0 /* PID, invalid. */,
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync VBoxServiceError("Control: Error sending start process status to host, rc=%Rrc\n", rc2);
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync * Gets output from stdout/stderr of a specified guest process.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return IPRT status code.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param uPID PID of process to retrieve the output from.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param uHandleId Stream ID (stdout = 0, stderr = 2) to get the output from.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param uTimeout Timeout (in ms) to wait for output becoming available.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * @param pvBuf Pointer to a pre-allocated buffer to store the output.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * @param cbBuf Size (in bytes) of the pre-allocated buffer.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync * @param pcbRead Pointer to number of bytes read. Optional.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsyncint VBoxServiceControlExecGetOutput(uint32_t uPID, uint32_t uCID,
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync /* pcbRead is optional. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlThreadRequestAllocEx(&pRequest, reqType,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VBoxServiceControlThreadPerform(uPID, pRequest);
65697a26b524640b83828b715160c798c43a0424vboxsync * Injects input to a specified running process.
65697a26b524640b83828b715160c798c43a0424vboxsync * @return IPRT status code.
65697a26b524640b83828b715160c798c43a0424vboxsync * @param uPID PID of process to set the input for.
65697a26b524640b83828b715160c798c43a0424vboxsync * @param fPendingClose Flag indicating whether this is the last input block sent to the process.
65697a26b524640b83828b715160c798c43a0424vboxsync * @param pvBuf Pointer to a buffer containing the actual input data.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param cbBuf Size (in bytes) of the input buffer data.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * @param pcbWritten Pointer to number of bytes written to the process. Optional.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncint VBoxServiceControlSetInput(uint32_t uPID, uint32_t uCID,
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pcbWritten is optional. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VBoxServiceControlThreadRequestAllocEx(&pRequest,
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync rc = VBoxServiceControlThreadPerform(uPID, pRequest);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Handles input for a started process by copying the received data into its
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * stdin pipe.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @returns IPRT status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param idClient The HGCM client session ID.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cParms The number of parameters the host is
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * offering.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cMaxBufSize The maximum buffer size for retrieving the input data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdSetInput(uint32_t idClient, uint32_t cParms, size_t cbMaxBufSize)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(RT_IS_POWER_OF_TWO(cbMaxBufSize), VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint8_t *pabBuffer = (uint8_t*)RTMemAlloc(cbMaxBufSize);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uStatus = INPUT_STS_UNDEFINED; /* Status sent back to the host. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t cbWritten = 0; /* Number of bytes written to the guest. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Ask the host for the input data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VbglR3GuestCtrlExecGetHostCmdInput(idClient, cParms,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceError("Control: [PID %u]: Failed to retrieve exec input command! Error: %Rrc\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VBoxServiceError("Control: [PID %u]: Too much input received! cbSize=%u, cbMaxBufSize=%u\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Is this the last input block we need to deliver? Then let the pipe know ...
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VBoxServiceVerbose(4, "Control: [PID %u]: Got last input block of size %u ...\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VBoxServiceControlSetInput(uPID, uContextID, fPendingClose, pabBuffer,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(4, "Control: [PID %u]: Written input, CID=%u, rc=%Rrc, uFlags=0x%x, fPendingClose=%d, cbSize=%u, cbWritten=%u\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uPID, uContextID, rc, uFlags, fPendingClose, cbSize, cbWritten);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (cbWritten || !cbSize) /* Did we write something or was there anything to write at all? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * If there was an error and we did not set the host status
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * yet, then do it now.
b0db50948c349fa76655abf252f7946b515e8204vboxsync VBoxServiceVerbose(3, "Control: [PID %u]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
b0db50948c349fa76655abf252f7946b515e8204vboxsync /* Note: Since the context ID is unique the request *has* to be completed here,
b0db50948c349fa76655abf252f7946b515e8204vboxsync * regardless whether we got data or not! Otherwise the progress object
b0db50948c349fa76655abf252f7946b515e8204vboxsync * on the host never will get completed! */
b0db50948c349fa76655abf252f7946b515e8204vboxsync rc = VbglR3GuestCtrlExecReportStatusIn(idClient, uContextID, uPID,
b0db50948c349fa76655abf252f7946b515e8204vboxsync VBoxServiceError("Control: [PID %u]: Failed to report input status! Error: %Rrc\n",
b0db50948c349fa76655abf252f7946b515e8204vboxsync * Handles the guest control output command.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return IPRT status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param idClient The HGCM client session ID.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cParms The number of parameters the host is offering.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdGetOutput(uint32_t idClient, uint32_t cParms)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VbglR3GuestCtrlExecGetHostCmdOutput(idClient, cParms,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlExecGetOutput(uPID, uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(3, "Control: [PID %u]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
faee255cc48bfbf17cb9f72fca70c8b9d3020ec4vboxsync /** Note: Don't convert/touch/modify/whatever the output data here! This might be binary
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * data which the host needs to work with -- so just pass through all data unfiltered! */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Note: Since the context ID is unique the request *has* to be completed here,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * regardless whether we got data or not! Otherwise the progress object
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * on the host never will get completed! */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = VbglR3GuestCtrlExecSendOut(idClient, uContextID, uPID, uHandleID, uFlags,
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync VBoxServiceError("Control: [PID %u]: Error handling output command! Error: %Rrc\n",
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync/** @copydoc VBOXSERVICE::pfnStop */
cab115cfa31c584def7069312a1e23c3fc88533bvboxsyncstatic DECLCALLBACK(void) VBoxServiceControlStop(void)
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync /** @todo Later, figure what to do if we're in RTProcWait(). It's a very
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * annoying call since doesn't support timeouts in the posix world. */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Ask the host service to cancel all pending requests so that we can
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * shutdown properly here.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Cancelling pending waits (client ID=%u) ...\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = VbglR3GuestCtrlCancelPendingWaits(g_GuestControlSvcClientID);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceError("Control: Cancelling pending waits failed; rc=%Rrc\n", rc);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * Destroys all guest process threads which are still active.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(2, "Control: Destroying threads ...\n");
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Signal all threads that we want to shutdown. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync RTListForEach(&g_GuestControlThreads, pThread, VBOXSERVICECTRLTHREAD, Node)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Wait for threads to shutdown and destroy thread list. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync pThread = RTListGetFirst(&g_GuestControlThreads, VBOXSERVICECTRLTHREAD, Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync bool fLast = RTListNodeIsLast(&g_GuestControlThreads, &pThread->Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync int rc2 = VBoxServiceControlThreadWaitForShutdown(pThread,
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceError("Control: Guest process thread failed to stop; rc=%Rrc\n", rc2);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync VBoxServiceVerbose(4, "Control: Guest process threads left=%u\n", cThreads);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync ("Guest process thread list still contains children when it should not\n"));
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Destroy critical section. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync/** @copydoc VBOXSERVICE::pfnTerm */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsyncstatic DECLCALLBACK(void) VBoxServiceControlTerm(void)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Terminating ...\n");
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Disconnecting client ID=%u ...\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * Determines whether starting a new guest process according to the
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * maximum number of concurrent guest processes defined is allowed or not.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @return IPRT status code.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pbAllowed True if starting (another) guest process
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * is allowed, false if not.
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic int VBoxServiceControlStartAllowed(bool *pbAllowed)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * Check if we're respecting our memory policy by checking
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * how many guest processes are started and served already.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync if (g_GuestControlProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync RTListForEach(&g_GuestControlThreads, pThread, VBOXSERVICECTRLTHREAD, Node)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync VBoxServiceVerbose(3, "Control: Maximum served guest processes set to %u, running=%u, stopped=%u\n",
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync g_GuestControlProcsMaxKept, uProcsRunning, uProcsStopped);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync int32_t iProcsLeft = (g_GuestControlProcsMaxKept - uProcsRunning - 1);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync VBoxServiceVerbose(3, "Control: Maximum running guest processes reached (%u)\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync * Finds a (formerly) started process given by its PID and locks it. Must be unlocked
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync * by the caller with VBoxServiceControlThreadUnlock().
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param uPID PID to search for.
26947320577c481b4afefdb0afbb855181e5b2e8vboxsyncPVBOXSERVICECTRLTHREAD VBoxServiceControlGetThreadLocked(uint32_t uPID)
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
65697a26b524640b83828b715160c798c43a0424vboxsync * Unlocks a previously locked guest process thread.
65697a26b524640b83828b715160c798c43a0424vboxsync * @param pThread Thread to unlock.
65697a26b524640b83828b715160c798c43a0424vboxsyncvoid VBoxServiceControlThreadUnlock(const PVBOXSERVICECTRLTHREAD pThread)
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync * Assigns a valid PID to a guest control thread and also checks if there already was
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * another (stale) guest process which was using that PID before and destroys it.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @return IPRT status code.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @param pThread Thread to assign PID to.
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync * @param uPID PID to assign to the specified guest control execution thread.
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsyncint VBoxServiceControlAssignPID(PVBOXSERVICECTRLTHREAD pThread, uint32_t uPID)
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync /* Search old threads using the desired PID and shut them down completely -- it's
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync * not used anymore. */
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync bool fTryAgain = false;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(2, "ControlThread: PID %u was used before, trying again with %u ...\n",
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync /* Assign PID to current thread. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTCritSectLeave(&g_GuestControlThreadsCritSect);
cba6719bd64ec749967bbe931230452664109857vboxsync * Removes the specified guest process thread from the global thread
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * @return IPRT status code.
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * @param pThread Thread to remove.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncvoid VBoxServiceControlRemoveThread(const PVBOXSERVICECTRLTHREAD pThread)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(4, "Control: Removing thread (PID: %u) from thread list\n",
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(4, "Control: Guest process threads left=%u\n", cThreads);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTCritSectLeave(&g_GuestControlThreadsCritSect);
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * The 'vminfo' service description.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pszName. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pszDescription. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync "Host-driven Guest Control",
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pszUsage. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync " [--control-interval <ms>] [--control-procs-max-kept <x>]\n"
b0db50948c349fa76655abf252f7946b515e8204vboxsync " [--control-procs-mem-std[in|out|err] <KB>]"
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pszOptions. */
b0db50948c349fa76655abf252f7946b515e8204vboxsync " --control-interval Specifies the interval at which to check for\n"
b0db50948c349fa76655abf252f7946b515e8204vboxsync " new control commands. The default is 1000 ms.\n"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync " --control-procs-max-kept\n"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync " Specifies how many started guest processes are\n"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync " kept into memory to work with. Default is 25.\n"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* methods */