VBoxServiceControl.cpp revision d7a909bc753ae3e2160db551a3a568e082594d9a
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/* $Id$ */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @file
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync * VBoxServiceControl - Host-driven Guest Control.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*
e64031e20c39650a7bc902a3e1aba613b9415deevboxsync * Copyright (C) 2011 Oracle Corporation
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *
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
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync* Header Files *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <iprt/asm.h>
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <iprt/assert.h>
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <iprt/getopt.h>
43747b1f0bc8302a238fb35e55857a5e9aa1933dvboxsync#include <iprt/mem.h>
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync#include <iprt/semaphore.h>
b0db50948c349fa76655abf252f7946b515e8204vboxsync#include <iprt/thread.h>
2f0d866e126dd288169fed591c259c1c6b4016e5vboxsync#include <VBox/VBoxGuestLib.h>
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include <VBox/HostServices/GuestControlSvc.h>
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync#include "VBoxServiceInternal.h"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#include "VBoxServiceUtils.h"
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncusing namespace guestControl;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/*******************************************************************************
f5e53763b0a581b0299e98028c6c52192eb06785vboxsync* Global Variables *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync*******************************************************************************/
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** The control interval (milliseconds). */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsyncstatic uint32_t g_cMsControlInterval = 0;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/** The semaphore we're blocking our main control thread on. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic RTSEMEVENTMULTI g_hControlEvent = NIL_RTSEMEVENTMULTI;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** The guest control service client ID. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsyncstatic uint32_t g_GuestControlSvcClientID = 0;
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. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic uint32_t g_GuestControlProcsMaxKept = 25;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** List of guest control threads (VBOXSERVICECTRLTHREAD). */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic RTLISTANCHOR g_GuestControlThreads;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** Critical section protecting g_GuestControlExecThreads. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic RTCRITSECT g_GuestControlThreadsCritSect;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
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
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/** @copydoc VBOXSERVICE::pfnPreInit */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic DECLCALLBACK(int) VBoxServiceControlPreInit(void)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#ifdef VBOX_WITH_GUEST_PROPS
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
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 */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t uGuestPropSvcClientID;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VbglR3GuestPropConnect(&uGuestPropSvcClientID);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_FAILURE(rc))
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync {
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync if (rc == VERR_HGCM_SERVICE_NOT_FOUND) /* Host service is not available. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(0, "Control: Guest property service is not available, skipping\n");
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync rc = VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Failed to connect to the guest property service! Error: %Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceReadPropUInt32(uGuestPropSvcClientID, "/VirtualBox/GuestAdd/VBoxService/--control-procs-max-kept",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &g_GuestControlProcsMaxKept, 0, UINT32_MAX - 1);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VbglR3GuestPropDisconnect(uGuestPropSvcClientID);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (rc == VERR_NOT_FOUND) /* If a value is not found, don't be sad! */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = VINF_SUCCESS;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync return rc;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync#else
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /* Nothing to do here yet. */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync return VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#endif
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync/** @copydoc VBOXSERVICE::pfnOption */
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsyncstatic DECLCALLBACK(int) VBoxServiceControlOption(const char **ppszShort, int argc, char **argv, int *pi)
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync{
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync int rc = -1;
65697a26b524640b83828b715160c798c43a0424vboxsync if (ppszShort)
65697a26b524640b83828b715160c798c43a0424vboxsync /* no short options */;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else if (!strcmp(argv[*pi], "--control-interval"))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VBoxServiceArgUInt32(argc, argv, "", pi,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &g_cMsControlInterval, 1, UINT32_MAX - 1);
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync else if (!strcmp(argv[*pi], "--control-procs-max-kept"))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceArgUInt32(argc, argv, "", pi,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &g_GuestControlProcsMaxKept, 0, UINT32_MAX - 1);
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync return rc;
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
0381007ae929f1a0885e69644b7d586a1dbb3a2avboxsync
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync/** @copydoc VBOXSERVICE::pfnInit */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsyncstatic DECLCALLBACK(int) VBoxServiceControlInit(void)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync{
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync /*
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * If not specified, find the right interval default.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync * Then create the event sem to block on.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync if (!g_cMsControlInterval)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync g_cMsControlInterval = 1000;
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = RTSemEventMultiCreate(&g_hControlEvent);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertRCReturn(rc, rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VbglR3GuestCtrlConnect(&g_GuestControlSvcClientID);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Service client ID: %#x\n", g_GuestControlSvcClientID);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Init thread list. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTListInit(&g_GuestControlThreads);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTCritSectInit(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
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 {
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync VBoxServiceVerbose(0, "Control: Guest control service is not available\n");
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync rc = VERR_SERVICE_DISABLED;
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Failed to connect to the guest control service! Error: %Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTSemEventMultiDestroy(g_hControlEvent);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync g_hControlEvent = NIL_RTSEMEVENTMULTI;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync}
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/** @copydoc VBOXSERVICE::pfnWorker */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncDECLCALLBACK(int) VBoxServiceControlWorker(bool volatile *pfShutdown)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /*
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Tell the control thread that it can continue
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * spawning services.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTThreadUserSignal(RTThreadSelf());
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync Assert(g_GuestControlSvcClientID > 0);
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VINF_SUCCESS;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /*
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Execution loop.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @todo
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync for (;;)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync VBoxServiceVerbose(3, "Control: Waiting for host msg ...\n");
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t uMsg;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t cParms;
93f91841f87620d1cb6d0238b3d0d5e52cd3b9a4vboxsync rc = VbglR3GuestCtrlWaitForHostMsg(g_GuestControlSvcClientID, &uMsg, &cParms);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (rc == VERR_TOO_MUCH_DATA)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
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. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else if (RT_FAILURE(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Getting host message failed with %Rrc\n", rc); /* VERR_GEN_IO_FAILURE seems to be normal if ran into timeout. */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
65697a26b524640b83828b715160c798c43a0424vboxsync VBoxServiceVerbose(3, "Control: Msg=%u (%u parms) retrieved\n", uMsg, cParms);
65697a26b524640b83828b715160c798c43a0424vboxsync switch (uMsg)
65697a26b524640b83828b715160c798c43a0424vboxsync {
65697a26b524640b83828b715160c798c43a0424vboxsync case HOST_CANCEL_PENDING_WAITS:
65697a26b524640b83828b715160c798c43a0424vboxsync VBoxServiceVerbose(3, "Control: Host asked us to quit ...\n");
65697a26b524640b83828b715160c798c43a0424vboxsync break;
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync case HOST_EXEC_CMD:
65697a26b524640b83828b715160c798c43a0424vboxsync rc = VBoxServiceControlHandleCmdStartProc(g_GuestControlSvcClientID, cParms);
65697a26b524640b83828b715160c798c43a0424vboxsync break;
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync case HOST_EXEC_SET_INPUT:
65697a26b524640b83828b715160c798c43a0424vboxsync /** @todo Make buffer size configurable via guest properties/argv! */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlHandleCmdSetInput(g_GuestControlSvcClientID, cParms, _1M /* Buffer size */);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync break;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync case HOST_EXEC_GET_OUTPUT:
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync rc = VBoxServiceControlHandleCmdGetOutput(g_GuestControlSvcClientID, cParms);
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync break;
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync default:
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(3, "Control: Unsupported message from host! Msg=%u\n", uMsg);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Don't terminate here; just wait for the next message. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
65697a26b524640b83828b715160c798c43a0424vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Do we need to shutdown? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if ( *pfShutdown
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync || uMsg == HOST_CANCEL_PENDING_WAITS)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
f687f34bd232be13744edbc0cc5155fa5d4540edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Let's sleep for a bit and let others run ... */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTThreadYield();
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync return rc;
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync}
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync/**
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * Handles starting processes on the guest.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync * @returns IPRT status code.
ead016c68c61b5f2e1fe4d237054eebea9327d4bvboxsync * @param idClient The HGCM client session ID.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cParms The number of parameters the host is offering.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync */
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsyncstatic int VBoxServiceControlHandleCmdStartProc(uint32_t idClient, uint32_t cParms)
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync{
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync uint32_t uContextID;
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync char szCmd[_1K];
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync uint32_t uFlags;
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync char szArgs[_1K];
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync uint32_t cArgs;
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync char szEnv[_64K];
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync uint32_t cbEnv = sizeof(szEnv);
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync uint32_t cEnvVars;
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync char szUser[128];
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync char szPassword[128];
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync uint32_t uTimeLimitMS;
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync#if 0 /* for valgrind */
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync RT_ZERO(szCmd);
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync RT_ZERO(szArgs);
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync RT_ZERO(szEnv);
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync RT_ZERO(szUser);
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync RT_ZERO(szPassword);
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync#endif
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync int rc;
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync bool fStartAllowed = false; /* Flag indicating whether starting a process is allowed or not. */
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync if (cParms == 11)
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync {
64f58e4154eaa20c47782b429eeaff09070369bfvboxsync rc = VbglR3GuestCtrlExecGetHostCmdExec(idClient,
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync cParms,
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync &uContextID,
7ccfefe49db4cd93c3701d7b60873ebf404b5b87vboxsync /* Command */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync szCmd, sizeof(szCmd),
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Flags */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync &uFlags,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Arguments */
65697a26b524640b83828b715160c798c43a0424vboxsync szArgs, sizeof(szArgs), &cArgs,
65697a26b524640b83828b715160c798c43a0424vboxsync /* Environment */
65697a26b524640b83828b715160c798c43a0424vboxsync szEnv, &cbEnv, &cEnvVars,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Credentials */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync szUser, sizeof(szUser),
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync szPassword, sizeof(szPassword),
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* Timelimit */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &uTimeLimitMS);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
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);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (fStartAllowed)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync /** @todo Put the following params into a struct! */
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync PRTLISTNODE pThreadNode;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync rc = VBoxServiceControlThreadStart(idClient, uContextID,
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync szCmd, uFlags, szArgs, cArgs,
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync szEnv, cbEnv, cEnvVars,
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync szUser, szPassword, uTimeLimitMS,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &pThreadNode);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* Insert thread node into thread list. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*rc =*/ RTListAppend(&g_GuestControlThreads, pThreadNode);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync }
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync if (RT_SUCCESS(rc))
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync rc = rc2;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_MAX_PROCS_REACHED; /* Maximum number of processes reached. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync else
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VERR_INVALID_PARAMETER; /* Incorrect number of parameters. */
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync /* In case of an error we need to notify the host to not wait forever for our response. */
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (RT_FAILURE(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceError("Control: Starting process failed with rc=%Rrc\n", rc);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc2 = VbglR3GuestCtrlExecReportStatus(idClient, uContextID, 0 /* PID, invalid. */,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PROC_STS_ERROR, rc,
a9f41cb889f53e8407561a6155052c441eb0fc5fvboxsync NULL /* pvData */, 0 /* cbData */);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_FAILURE(rc2))
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync {
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync VBoxServiceError("Control: Error sending start process status to host, rc=%Rrc\n", rc2);
0dd6dfbebcda0af90da4413aaea5f3b9d1817556vboxsync if (RT_SUCCESS(rc))
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync rc = rc2;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync }
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync }
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync return rc;
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync}
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync/**
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync * Gets output from stdout/stderr of a specified guest process.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync *
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.
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync */
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsyncint VBoxServiceControlExecGetOutput(uint32_t uPID, uint32_t uCID,
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync uint32_t uHandleId, uint32_t uTimeout,
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync void *pvBuf, uint32_t cbBuf, uint32_t *pcbRead)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync{
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertPtrReturn(pvBuf, VERR_INVALID_POINTER);
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync /* pcbRead is optional. */
c2f2661efd8da5281e2a3af6ddd10e737d333909vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync int rc = VINF_SUCCESS;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync VBOXSERVICECTRLREQUESTTYPE reqType;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync switch (uHandleId)
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync {
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync case OUTPUT_HANDLE_ID_STDERR:
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync reqType = VBOXSERVICECTRLREQUEST_STDERR_READ;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync break;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync case OUTPUT_HANDLE_ID_STDOUT:
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync case OUTPUT_HANDLE_ID_STDOUT_DEPRECATED:
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync reqType = VBOXSERVICECTRLREQUEST_STDOUT_READ;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync break;
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync
4569bf0ad094b40d2e177299a00d37e94d28616cvboxsync default:
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync rc = VERR_INVALID_PARAMETER;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync break;
fc78e01f665145ab3641c5f8095e9ae984ddcb84vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync PVBOXSERVICECTRLREQUEST pRequest;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlThreadRequestAllocEx(&pRequest, reqType,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pvBuf, cbBuf, uCID);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VBoxServiceControlThreadPerform(uPID, pRequest);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
65697a26b524640b83828b715160c798c43a0424vboxsync if (pcbRead)
65697a26b524640b83828b715160c798c43a0424vboxsync *pcbRead = pRequest->cbData;
65697a26b524640b83828b715160c798c43a0424vboxsync }
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync VBoxServiceControlThreadRequestFree(pRequest);
65697a26b524640b83828b715160c798c43a0424vboxsync }
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync return rc;
65697a26b524640b83828b715160c798c43a0424vboxsync}
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync/**
65697a26b524640b83828b715160c798c43a0424vboxsync * Injects input to a specified running process.
65697a26b524640b83828b715160c798c43a0424vboxsync *
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.
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncint VBoxServiceControlSetInput(uint32_t uPID, uint32_t uCID,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync bool fPendingClose,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync void *pvBuf, uint32_t cbBuf,
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync uint32_t *pcbWritten)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync AssertPtrReturn(pvBuf, VERR_INVALID_PARAMETER);
96a7e06717e2d7398642eadb5ebab1bf13fbe2dbvboxsync AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pcbWritten is optional. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync PVBOXSERVICECTRLREQUEST pRequest;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VBoxServiceControlThreadRequestAllocEx(&pRequest,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fPendingClose
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync ? VBOXSERVICECTRLREQUEST_STDIN_WRITE_EOF
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync : VBOXSERVICECTRLREQUEST_STDIN_WRITE,
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync pvBuf, cbBuf, uCID);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync if (RT_SUCCESS(rc))
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync {
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync rc = VBoxServiceControlThreadPerform(uPID, pRequest);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync if (RT_SUCCESS(rc))
0c4004948fca34f2db87e7b38013137e9472c306vboxsync {
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync if (pcbWritten)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *pcbWritten = pRequest->cbData;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync }
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync VBoxServiceControlThreadRequestFree(pRequest);
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * Handles input for a started process by copying the received data into its
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * stdin pipe.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *
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.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdSetInput(uint32_t idClient, uint32_t cParms, size_t cbMaxBufSize)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync uint32_t uContextID;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uPID;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uFlags;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t cbSize;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertReturn(RT_IS_POWER_OF_TWO(cbMaxBufSize), VERR_INVALID_PARAMETER);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint8_t *pabBuffer = (uint8_t*)RTMemAlloc(cbMaxBufSize);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertPtrReturn(pabBuffer, VERR_NO_MEMORY);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
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
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * Ask the host for the input data.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = VbglR3GuestCtrlExecGetHostCmdInput(idClient, cParms,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync &uContextID, &uPID, &uFlags,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pabBuffer, cbMaxBufSize, &cbSize);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_FAILURE(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceError("Control: [PID %u]: Failed to retrieve exec input command! Error: %Rrc\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uPID, rc);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else if (cbSize > cbMaxBufSize)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VBoxServiceError("Control: [PID %u]: Too much input received! cbSize=%u, cbMaxBufSize=%u\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uPID, cbSize, cbMaxBufSize);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync rc = VERR_INVALID_PARAMETER;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync /*
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Is this the last input block we need to deliver? Then let the pipe know ...
8a339f91959bb7a3315b51a23461b68c7b0cb50evboxsync */
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync bool fPendingClose = false;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (uFlags & INPUT_FLAG_EOF)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync fPendingClose = true;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VBoxServiceVerbose(4, "Control: [PID %u]: Got last input block of size %u ...\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uPID, cbSize);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = VBoxServiceControlSetInput(uPID, uContextID, fPendingClose, pabBuffer,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync cbSize, &cbWritten);
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);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (cbWritten || !cbSize) /* Did we write something or was there anything to write at all? */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uStatus = INPUT_STS_WRITTEN;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uFlags = 0;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync else
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (rc == VERR_BAD_PIPE)
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync uStatus = INPUT_STS_TERMINATED;
d7125f3a1b435761c393f9ec406e85a73ae2a3e7vboxsync else if (rc == VERR_BUFFER_OVERFLOW)
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync uStatus = INPUT_STS_OVERFLOW;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync RTMemFree(pabBuffer);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /*
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * If there was an error and we did not set the host status
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * yet, then do it now.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if ( RT_FAILURE(rc)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync && uStatus == INPUT_STS_UNDEFINED)
b0db50948c349fa76655abf252f7946b515e8204vboxsync {
b0db50948c349fa76655abf252f7946b515e8204vboxsync uStatus = INPUT_STS_ERROR;
b0db50948c349fa76655abf252f7946b515e8204vboxsync uFlags = rc;
b0db50948c349fa76655abf252f7946b515e8204vboxsync }
b0db50948c349fa76655abf252f7946b515e8204vboxsync Assert(uStatus > INPUT_STS_UNDEFINED);
b0db50948c349fa76655abf252f7946b515e8204vboxsync
b0db50948c349fa76655abf252f7946b515e8204vboxsync VBoxServiceVerbose(3, "Control: [PID %u]: Input processed, CID=%u, uStatus=%u, uFlags=0x%x, cbWritten=%u\n",
b0db50948c349fa76655abf252f7946b515e8204vboxsync uPID, uContextID, uStatus, uFlags, cbWritten);
b0db50948c349fa76655abf252f7946b515e8204vboxsync
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 uStatus, uFlags, (uint32_t)cbWritten);
b0db50948c349fa76655abf252f7946b515e8204vboxsync
b0db50948c349fa76655abf252f7946b515e8204vboxsync if (RT_FAILURE(rc))
b0db50948c349fa76655abf252f7946b515e8204vboxsync VBoxServiceError("Control: [PID %u]: Failed to report input status! Error: %Rrc\n",
b0db50948c349fa76655abf252f7946b515e8204vboxsync uPID, rc);
b0db50948c349fa76655abf252f7946b515e8204vboxsync return rc;
b0db50948c349fa76655abf252f7946b515e8204vboxsync}
b0db50948c349fa76655abf252f7946b515e8204vboxsync
b0db50948c349fa76655abf252f7946b515e8204vboxsync
b0db50948c349fa76655abf252f7946b515e8204vboxsync/**
b0db50948c349fa76655abf252f7946b515e8204vboxsync * Handles the guest control output command.
b0db50948c349fa76655abf252f7946b515e8204vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return IPRT status code.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param idClient The HGCM client session ID.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param cParms The number of parameters the host is offering.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsyncstatic int VBoxServiceControlHandleCmdGetOutput(uint32_t idClient, uint32_t cParms)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t uContextID;
b0db50948c349fa76655abf252f7946b515e8204vboxsync uint32_t uPID;
b0db50948c349fa76655abf252f7946b515e8204vboxsync uint32_t uHandleID;
b0db50948c349fa76655abf252f7946b515e8204vboxsync uint32_t uFlags;
b0db50948c349fa76655abf252f7946b515e8204vboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = VbglR3GuestCtrlExecGetHostCmdOutput(idClient, cParms,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync &uContextID, &uPID, &uHandleID, &uFlags);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint8_t *pBuf = (uint8_t*)RTMemAlloc(_64K);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (pBuf)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uint32_t cbRead = 0;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync rc = VBoxServiceControlExecGetOutput(uPID, uContextID, uHandleID, RT_INDEFINITE_WAIT /* Timeout */,
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pBuf, _64K /* cbSize */, &cbRead);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(3, "Control: [PID %u]: Got output, rc=%Rrc, CID=%u, cbRead=%u, uHandle=%u, uFlags=%u\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync uPID, rc, uContextID, cbRead, uHandleID, uFlags);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
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! */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
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,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pBuf, cbRead);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync if (RT_SUCCESS(rc))
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync rc = rc2;
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync else if (rc == VERR_NOT_FOUND) /* It's not critical if guest process (PID) is not found. */
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync rc = VINF_SUCCESS;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTMemFree(pBuf);
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync }
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync else
a39ea3668b7019c23a68936259545f9b71bce1aavboxsync rc = VERR_NO_MEMORY;
0db6a029780d9f9b347500e117320a8d5661efe5vboxsync }
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_FAILURE(rc))
ee4d840f54fd2dcea8a73b1b86d5ec0db370b05dvboxsync VBoxServiceError("Control: [PID %u]: Error handling output command! Error: %Rrc\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uPID, rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return rc;
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync}
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync/** @copydoc VBOXSERVICE::pfnStop */
cab115cfa31c584def7069312a1e23c3fc88533bvboxsyncstatic DECLCALLBACK(void) VBoxServiceControlStop(void)
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync{
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync VBoxServiceVerbose(3, "Control: Stopping ...\n");
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync
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. */
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync RTSemEventMultiSignal(g_hControlEvent);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync * Ask the host service to cancel all pending requests so that we can
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * shutdown properly here.
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsync if (g_GuestControlSvcClientID)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync {
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Cancelling pending waits (client ID=%u) ...\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync g_GuestControlSvcClientID);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync int rc = VbglR3GuestCtrlCancelPendingWaits(g_GuestControlSvcClientID);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync if (RT_FAILURE(rc))
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceError("Control: Cancelling pending waits failed; rc=%Rrc\n", rc);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync }
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync}
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync/**
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * Destroys all guest process threads which are still active.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic void VBoxServiceControlDestroyThreads(void)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync{
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(2, "Control: Destroying threads ...\n");
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Signal all threads that we want to shutdown. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync PVBOXSERVICECTRLTHREAD pThread;
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync RTListForEach(&g_GuestControlThreads, pThread, VBOXSERVICECTRLTHREAD, Node)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceControlThreadSignalShutdown(pThread);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Wait for threads to shutdown and destroy thread list. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync pThread = RTListGetFirst(&g_GuestControlThreads, VBOXSERVICECTRLTHREAD, Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync while (pThread)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync {
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync PVBOXSERVICECTRLTHREAD pNext = RTListNodeGetNext(&pThread->Node, VBOXSERVICECTRLTHREAD, Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync bool fLast = RTListNodeIsLast(&g_GuestControlThreads, &pThread->Node);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync int rc2 = VBoxServiceControlThreadWaitForShutdown(pThread,
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync 30 * 1000 /* Wait 30 seconds max. */);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync if (RT_FAILURE(rc2))
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceError("Control: Guest process thread failed to stop; rc=%Rrc\n", rc2);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync if (fLast)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync break;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync pThread = pNext;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync }
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync#ifdef DEBUG
0c4004948fca34f2db87e7b38013137e9472c306vboxsync PVBOXSERVICECTRLTHREAD pThreadCur;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync uint32_t cThreads = 0;
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync cThreads++;
0c4004948fca34f2db87e7b38013137e9472c306vboxsync VBoxServiceVerbose(4, "Control: Guest process threads left=%u\n", cThreads);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync#endif
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertMsg(RTListIsEmpty(&g_GuestControlThreads),
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync ("Guest process thread list still contains children when it should not\n"));
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* Destroy critical section. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync RTCritSectDelete(&g_GuestControlThreadsCritSect);
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync}
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync/** @copydoc VBOXSERVICE::pfnTerm */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsyncstatic DECLCALLBACK(void) VBoxServiceControlTerm(void)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync{
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Terminating ...\n");
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceControlDestroyThreads();
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceVerbose(3, "Control: Disconnecting client ID=%u ...\n",
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync g_GuestControlSvcClientID);
d4e9ccea0ea1ed303b5708ff94f6c202755f0dc6vboxsync VbglR3GuestCtrlDisconnect(g_GuestControlSvcClientID);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync g_GuestControlSvcClientID = 0;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (g_hControlEvent != NIL_RTSEMEVENTMULTI)
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync RTSemEventMultiDestroy(g_hControlEvent);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync g_hControlEvent = NIL_RTSEMEVENTMULTI;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync }
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync}
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync/**
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync * Determines whether starting a new guest process according to the
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * maximum number of concurrent guest processes defined is allowed or not.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync *
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @return IPRT status code.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * @param pbAllowed True if starting (another) guest process
0c4004948fca34f2db87e7b38013137e9472c306vboxsync * is allowed, false if not.
0c4004948fca34f2db87e7b38013137e9472c306vboxsync */
0c4004948fca34f2db87e7b38013137e9472c306vboxsyncstatic int VBoxServiceControlStartAllowed(bool *pbAllowed)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync{
0c4004948fca34f2db87e7b38013137e9472c306vboxsync AssertPtrReturn(pbAllowed, VERR_INVALID_POINTER);
0c4004948fca34f2db87e7b38013137e9472c306vboxsync
0c4004948fca34f2db87e7b38013137e9472c306vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (RT_SUCCESS(rc))
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync {
0c4004948fca34f2db87e7b38013137e9472c306vboxsync /*
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * Check if we're respecting our memory policy by checking
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync * how many guest processes are started and served already.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync bool fLimitReached = false;
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync if (g_GuestControlProcsMaxKept) /* If we allow unlimited processes (=0), take a shortcut. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync {
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync uint32_t uProcsRunning = 0;
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync uint32_t uProcsStopped = 0;
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync PVBOXSERVICECTRLTHREAD pThread;
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync RTListForEach(&g_GuestControlThreads, pThread, VBOXSERVICECTRLTHREAD, Node)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync {
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync VBoxServiceControlThreadActive(pThread)
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync ? uProcsRunning++
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync : uProcsStopped++;
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync }
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync VBoxServiceVerbose(3, "Control: Maximum served guest processes set to %u, running=%u, stopped=%u\n",
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync g_GuestControlProcsMaxKept, uProcsRunning, uProcsStopped);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync int32_t iProcsLeft = (g_GuestControlProcsMaxKept - uProcsRunning - 1);
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync if (iProcsLeft < 0)
0c4004948fca34f2db87e7b38013137e9472c306vboxsync {
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync VBoxServiceVerbose(3, "Control: Maximum running guest processes reached (%u)\n",
8f0fc87a72dee210b62acc9dd859a4bebf8bfb33vboxsync g_GuestControlProcsMaxKept);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync fLimitReached = true;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync *pbAllowed = !fLimitReached;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = rc2;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync/**
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync * Finds a (formerly) started process given by its PID and locks it. Must be unlocked
8cd2f2e64725096acb682f34a5568b7fb816eda7vboxsync * by the caller with VBoxServiceControlThreadUnlock().
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync *
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @return PVBOXSERVICECTRLTHREAD Process structure if found, otherwise NULL.
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * @param uPID PID to search for.
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync */
26947320577c481b4afefdb0afbb855181e5b2e8vboxsyncPVBOXSERVICECTRLTHREAD VBoxServiceControlGetThreadLocked(uint32_t uPID)
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync{
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync PVBOXSERVICECTRLTHREAD pThread = NULL;
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync if (RT_SUCCESS(rc))
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync {
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync PVBOXSERVICECTRLTHREAD pThreadCur;
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync {
26947320577c481b4afefdb0afbb855181e5b2e8vboxsync if (pThreadCur->uPID == uPID)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTCritSectEnter(&pThreadCur->CritSect);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pThread = pThreadCur;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
cba6719bd64ec749967bbe931230452664109857vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc2 = RTCritSectLeave(&g_GuestControlThreadsCritSect);
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync if (RT_SUCCESS(rc))
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = rc2;
c28fa006ba669ad8f26ae31d00a338379c04ea1bvboxsync }
e74eef731a813e4e06680c587a6759b9974b29c9vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync return pThread;
65697a26b524640b83828b715160c798c43a0424vboxsync}
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync
65697a26b524640b83828b715160c798c43a0424vboxsync/**
65697a26b524640b83828b715160c798c43a0424vboxsync * Unlocks a previously locked guest process thread.
65697a26b524640b83828b715160c798c43a0424vboxsync *
65697a26b524640b83828b715160c798c43a0424vboxsync * @param pThread Thread to unlock.
65697a26b524640b83828b715160c798c43a0424vboxsync */
65697a26b524640b83828b715160c798c43a0424vboxsyncvoid VBoxServiceControlThreadUnlock(const PVBOXSERVICECTRLTHREAD pThread)
65697a26b524640b83828b715160c798c43a0424vboxsync{
65697a26b524640b83828b715160c798c43a0424vboxsync AssertPtr(pThread);
65697a26b524640b83828b715160c798c43a0424vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync int rc = RTCritSectLeave(&pThread->CritSect);
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync
a8139954a84d6e9090dd3a8371aa788351d45bc3vboxsync/**
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.
cab115cfa31c584def7069312a1e23c3fc88533bvboxsync *
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.
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync */
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsyncint VBoxServiceControlAssignPID(PVBOXSERVICECTRLTHREAD pThread, uint32_t uPID)
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync{
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync AssertPtrReturn(pThread, VERR_INVALID_POINTER);
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync AssertReturn(uPID, VERR_INVALID_PARAMETER);
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync if (RT_SUCCESS(rc))
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync {
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync /* Search old threads using the desired PID and shut them down completely -- it's
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync * not used anymore. */
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync PVBOXSERVICECTRLTHREAD pThreadCur;
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync bool fTryAgain = false;
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync do
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync if (pThreadCur->uPID == uPID)
4c42ef40805493bd6d76103b90f71bcd6dbc0f00vboxsync {
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync Assert(pThreadCur != pThread); /* can't happen */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uint32_t uTriedPID = uPID;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uPID += 391939;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(2, "ControlThread: PID %u was used before, trying again with %u ...\n",
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync uTriedPID, uPID);
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync fTryAgain = true;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync break;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync } while (fTryAgain);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync /* Assign PID to current thread. */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync pThread->uPID = uPID;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTCritSectLeave(&g_GuestControlThreadsCritSect);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync return rc;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync}
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
da3503c04ce76e653401396fe2795a9bc2427a1dvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync/**
cba6719bd64ec749967bbe931230452664109857vboxsync * Removes the specified guest process thread from the global thread
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync * list.
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync *
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * @return IPRT status code.
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync * @param pThread Thread to remove.
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync */
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsyncvoid VBoxServiceControlRemoveThread(const PVBOXSERVICECTRLTHREAD pThread)
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync{
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (!pThread)
65697a26b524640b83828b715160c798c43a0424vboxsync return;
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync int rc = RTCritSectEnter(&g_GuestControlThreadsCritSect);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync if (RT_SUCCESS(rc))
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync {
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync VBoxServiceVerbose(4, "Control: Removing thread (PID: %u) from thread list\n",
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync pThread->uPID);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync RTListNodeRemove(&pThread->Node);
ea779b55cc87f3e3fadddca4672c6697c82606edvboxsync
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync#ifdef DEBUG
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync PVBOXSERVICECTRLTHREAD pThreadCur;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync uint32_t cThreads = 0;
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync RTListForEach(&g_GuestControlThreads, pThreadCur, VBOXSERVICECTRLTHREAD, Node)
9b789c281103a2489742bf32f6ab500e38b2ecd5vboxsync cThreads++;
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceVerbose(4, "Control: Guest process threads left=%u\n", cThreads);
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync#endif
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync rc = RTCritSectLeave(&g_GuestControlThreadsCritSect);
c91345f92b829d3fba05ce7a97206d83c5183ce0vboxsync AssertRC(rc);
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync }
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync}
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync/**
44a2ecaf2d0fc196ab76cab13b3f909299e386d1vboxsync * The 'vminfo' service description.
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsyncVBOXSERVICE g_Control =
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync{
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync /* pszName. */
71f6a34b72f9cc873da208630959de49df1a28a5vboxsync "control",
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>]"
b0db50948c349fa76655abf252f7946b515e8204vboxsync ,
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 ,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync /* methods */
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlPreInit,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlOption,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlInit,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlWorker,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlStop,
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync VBoxServiceControlTerm
5b465a7c1237993faf8bb50120d247f3f0319adavboxsync};
9dca051a5f8ff457ef1692990f6ecfa280daf265vboxsync
ad77e3ec3cde24263bc7537575f5cae442bee3b1vboxsync