SUPSvc.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/* $Id$ */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/** @file
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * VirtualBox Support Service - Common Code.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/*
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Copyright (C) 2008 Oracle Corporation
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * available from http://www.virtualbox.org. This file is free software;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * you can redistribute it and/or modify it under the terms of the GNU
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * General Public License (GPL) as published by the Free Software
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * The contents of this file may alternatively be used under the terms
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * of the Common Development and Distribution License Version 1.0
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * (CDDL) only, as it comes in the "COPYING.CDDL" file of the
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * VirtualBox OSE distribution, in which case the provisions of the
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * CDDL are applicable instead of those of the GPL.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * You may elect to license modified versions of this file under the
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * terms and conditions of either the GPL or the CDDL or both.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/*******************************************************************************
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync* Header Files *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync*******************************************************************************/
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#define LOG_GROUP LOG_GROUP_SUP
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include <VBox/log.h>
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include <iprt/string.h>
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include <iprt/mem.h>
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include <iprt/stream.h>
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include <iprt/getopt.h>
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#include "SUPSvcInternal.h"
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/*******************************************************************************
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync* Structures and Typedefs *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync*******************************************************************************/
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Service state.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsynctypedef enum SUPSVCSERVICESTATE
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync{
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_Invalid = 0,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_NotCreated,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_Paused,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_Running,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_End
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync} SUPSVCSERVICESTATE;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Service descriptor.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsynctypedef struct SUPSVCSERVICE
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync{
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /** The service name. */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync const char *pszName;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /** The service state. */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync SUPSVCSERVICESTATE enmState;
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync /** The instance handle returned by pfnCreate. */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync void *pvInstance;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Create the service (don't start it).
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @returns VBox status code, log entry is written on failure.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @param ppvInstance Where to store the instance handle.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync DECLCALLBACKMEMBER(int, pfnCreate)(void **ppvInstance);
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Start the service.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @returns VBox status code, log entry is written on failure.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @param pvInstance The instance handle.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync DECLCALLBACKMEMBER(void, pfnStart)(void *pvInstance);
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Attempt to stop a running service.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * This should fail if there are active clients. A stopped service
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * can be restarted by calling pfnStart.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @returns VBox status code, log entry is written on failure.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @param pvInstance The instance handle.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync DECLCALLBACKMEMBER(int, pfnTryStop)(void *pvInstance);
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync /**
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * Destroy the service, stopping first it if necessary.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync *
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @param pvInstance The instance handle.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync * @param fRunning Whether the service is running or not.
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync DECLCALLBACKMEMBER(void, pfnStopAndDestroy)(void *pvInstance, bool fRunning);
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync} SUPSVCSERVICE;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/** Pointer to a service descriptor. */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsynctypedef SUPSVCSERVICE *PSUPSVCSERVICE;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/** Pointer to a const service descriptor. */
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsynctypedef SUPSVCSERVICE const *PCSUPSVCSERVICE;
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync/*******************************************************************************
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync* Global Variables *
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync*******************************************************************************/
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsyncstatic SUPSVCSERVICE g_aServices[] =
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync{
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync {
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync "Global",
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync kSupSvcServiceState_NotCreated,
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync NULL,
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync supSvcGlobalCreate,
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync supSvcGlobalStart,
8d82d07fc2f7234b1e5a3ba544e1086a5a7a7c5fvboxsync supSvcGlobalTryStop,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync supSvcGlobalStopAndDestroy,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync }
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync#ifdef RT_OS_WINDOWS
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync ,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync {
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync "Grant",
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync kSupSvcServiceState_NotCreated,
73045e3d71f04f3d399e70e89fbd6395c4402821vboxsync NULL,
supSvcGrantCreate,
supSvcGrantStart,
supSvcGrantTryStop,
supSvcGrantStopAndDestroy,
}
#endif
};
/**
* Instantiates and starts the services.
*
* @returns VBox status code. Done bitching on failure.
*/
int supSvcCreateAndStartServices(void)
{
LogFlowFuncEnter();
/*
* Validate that all services are in the NotCreated state.
*/
unsigned i;
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
if (g_aServices[i].enmState != kSupSvcServiceState_NotCreated)
{
supSvcLogError("service %s in state %d, expected state %d (NotCreated)",
g_aServices[i].pszName, g_aServices[i].enmState, kSupSvcServiceState_NotCreated);
return VERR_WRONG_ORDER;
}
/*
* Create all the services, then start them.
*/
int rc = VINF_SUCCESS;
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
{
void *pvInstance = NULL;
int rc = g_aServices[i].pfnCreate(&pvInstance);
if (RT_FAILURE(rc))
{
Log(("supSvcCreateAndStartServices: %s -> %Rrc\n", g_aServices[i].pszName, rc));
break;
}
g_aServices[i].pvInstance = pvInstance;
g_aServices[i].enmState = kSupSvcServiceState_Paused;
}
if (RT_SUCCESS(rc))
{
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
{
g_aServices[i].pfnStart(g_aServices[i].pvInstance);
g_aServices[i].enmState = kSupSvcServiceState_Running;
}
}
else
{
/*
* Destroy any services we managed to instantiate.
*/
while (i-- > 0)
{
g_aServices[i].pfnStopAndDestroy(g_aServices[i].pvInstance, false /* fRunning */);
g_aServices[i].pvInstance = NULL;
g_aServices[i].enmState = kSupSvcServiceState_NotCreated;
}
}
LogFlow(("supSvcCreateAndStartServices: returns %Rrc\n", rc));
return rc;
}
/**
* Checks if it's possible to stop the services.
*
* @returns VBox status code, done bitching on failure.
*/
int supSvcTryStopServices(void)
{
LogFlowFuncEnter();
/*
* Check that the services are all created and count the running ones.
*/
unsigned i;
unsigned cRunning = 0;
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
if (g_aServices[i].enmState == kSupSvcServiceState_Running)
cRunning++;
else if (g_aServices[i].enmState == kSupSvcServiceState_NotCreated)
{
supSvcLogError("service %s in state %d (NotCreated), expected pause or running",
g_aServices[i].pszName, g_aServices[i].enmState, kSupSvcServiceState_NotCreated);
return VERR_WRONG_ORDER;
}
if (!cRunning)
return VINF_SUCCESS; /* all stopped, nothing to do. */
Assert(cRunning == RT_ELEMENTS(g_aServices)); /* all or nothing */
/*
* Try stop them in reverse of start order.
*/
int rc = VINF_SUCCESS;
i = RT_ELEMENTS(g_aServices);
while (i-- > 0)
{
rc = g_aServices[i].pfnTryStop(g_aServices[i].pvInstance);
if (RT_FAILURE(rc))
{
Log(("supSvcTryStopServices: %s -> %Rrc\n", g_aServices[i].pszName, rc));
break;
}
g_aServices[i].enmState = kSupSvcServiceState_Paused;
}
if (RT_FAILURE(rc))
{
/* Failed, restart the ones we succeeded in stopping. */
while (++i < RT_ELEMENTS(g_aServices))
{
g_aServices[i].pfnStart(g_aServices[i].pvInstance);
g_aServices[i].enmState = kSupSvcServiceState_Running;
}
}
LogFlow(("supSvcTryStopServices: returns %Rrc\n", rc));
return rc;
}
/**
* Stops and destroys the services.
*/
void supSvcStopAndDestroyServices(void)
{
LogFlowFuncEnter();
/*
* Stop and destroy the service in reverse of start order.
*/
unsigned i = RT_ELEMENTS(g_aServices);
while (i-- > 0)
if (g_aServices[i].enmState != kSupSvcServiceState_NotCreated)
{
g_aServices[i].pfnStopAndDestroy(g_aServices[i].pvInstance,
g_aServices[i].enmState == kSupSvcServiceState_Running);
g_aServices[i].pvInstance = NULL;
g_aServices[i].enmState = kSupSvcServiceState_NotCreated;
}
LogFlowFuncLeave();
}
/**
* Logs the message to the appropirate system log.
*
* In debug builds this will also put it in the debug log.
*
* @param pszMsg The log string.
*
* @remarks This may later be replaced by the release logger and callback destination(s).
*/
void supSvcLogErrorStr(const char *pszMsg)
{
supSvcOsLogErrorStr(pszMsg);
LogRel(("%s\n", pszMsg));
}
/**
* Logs the message to the appropirate system log.
*
* In debug builds this will also put it in the debug log.
*
* @param pszFormat The log string. No trailing newline.
* @param ... Format arguments.
*
* @todo This should later be replaced by the release logger and callback destination(s).
*/
void supSvcLogErrorV(const char *pszFormat, va_list va)
{
if (*pszFormat)
{
char *pszMsg = NULL;
if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
{
supSvcLogErrorStr(pszMsg);
RTStrFree(pszMsg);
}
else
supSvcLogErrorStr(pszFormat);
}
}
/**
* Logs the error message to the appropirate system log.
*
* In debug builds this will also put it in the debug log.
*
* @param pszFormat The log string. No trailing newline.
* @param ... Format arguments.
*
* @todo This should later be replaced by the release logger and callback destination(s).
*/
void supSvcLogError(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
supSvcLogErrorV(pszFormat, va);
va_end(va);
}
/**
* Deals with RTGetOpt failure, bitching in the system log.
*
* @returns 1
* @param pszAction The action name.
* @param rc The RTGetOpt return value.
* @param argc The argument count.
* @param argv The argument vector.
* @param iArg The argument index.
* @param pValue The value returned by RTGetOpt.
*/
int supSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTOPTIONUNION pValue)
{
supSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s",
pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
return 1;
}
/**
* Bitch about too many arguments (after RTGetOpt stops) in the system log.
*
* @returns 1
* @param pszAction The action name.
* @param argc The argument count.
* @param argv The argument vector.
* @param iArg The argument index.
*/
int supSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
{
Assert(iArg < argc);
supSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
for ( ; iArg < argc; iArg++)
LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
return 1;
}
/**
* Prints an error message to the screen.
*
* @param pszFormat The message format string.
* @param va Format arguments.
*/
void supSvcDisplayErrorV(const char *pszFormat, va_list va)
{
RTStrmPrintf(g_pStdErr, "VBoxSupSvc error: ");
RTStrmPrintfV(g_pStdErr, pszFormat, va);
Log(("supSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
}
/**
* Prints an error message to the screen.
*
* @param pszFormat The message format string.
* @param ... Format arguments.
*/
void supSvcDisplayError(const char *pszFormat, ...)
{
va_list va;
va_start(va, pszFormat);
supSvcDisplayErrorV(pszFormat, va);
va_end(va);
}
/**
* Deals with RTGetOpt failure.
*
* @returns 1
* @param pszAction The action name.
* @param rc The RTGetOpt return value.
* @param argc The argument count.
* @param argv The argument vector.
* @param iArg The argument index.
* @param pValue The value returned by RTGetOpt.
*/
int supSvcDisplayGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTOPTIONUNION pValue)
{
supSvcDisplayError("%s - RTGetOpt failure, %Rrc (%d): %s\n",
pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
return 1;
}
/**
* Bitch about too many arguments (after RTGetOpt stops).
*
* @returns 1
* @param pszAction The action name.
* @param argc The argument count.
* @param argv The argument vector.
* @param iArg The argument index.
*/
int supSvcDisplayTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
{
Assert(iArg < argc);
supSvcDisplayError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);
return 1;
}