SUPSvc.cpp revision e64031e20c39650a7bc902a3e1aba613b9415dee
/* $Id$ */
/** @file
* VirtualBox Support Service - Common Code.
*/
/*
* Copyright (C) 2008 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*
* The contents of this file may alternatively be used under the terms
* of the Common Development and Distribution License Version 1.0
* (CDDL) only, as it comes in the "COPYING.CDDL" file of the
* VirtualBox OSE distribution, in which case the provisions of the
* CDDL are applicable instead of those of the GPL.
*
* You may elect to license modified versions of this file under the
* terms and conditions of either the GPL or the CDDL or both.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#define LOG_GROUP LOG_GROUP_SUP
#include "SUPSvcInternal.h"
/*******************************************************************************
* Structures and Typedefs *
*******************************************************************************/
/**
* Service state.
*/
typedef enum SUPSVCSERVICESTATE
{
/**
* Service descriptor.
*/
typedef struct SUPSVCSERVICE
{
/** The service name. */
const char *pszName;
/** The service state. */
/** The instance handle returned by pfnCreate. */
void *pvInstance;
/**
* Create the service (don't start it).
*
* @returns VBox status code, log entry is written on failure.
* @param ppvInstance Where to store the instance handle.
*/
/**
* Start the service.
*
* @returns VBox status code, log entry is written on failure.
* @param pvInstance The instance handle.
*/
/**
* Attempt to stop a running service.
*
* This should fail if there are active clients. A stopped service
* can be restarted by calling pfnStart.
*
* @returns VBox status code, log entry is written on failure.
* @param pvInstance The instance handle.
*/
/**
* Destroy the service, stopping first it if necessary.
*
* @param pvInstance The instance handle.
* @param fRunning Whether the service is running or not.
*/
/** Pointer to a service descriptor. */
typedef SUPSVCSERVICE *PSUPSVCSERVICE;
/** Pointer to a const service descriptor. */
typedef SUPSVCSERVICE const *PCSUPSVCSERVICE;
/*******************************************************************************
* Global Variables *
*******************************************************************************/
static SUPSVCSERVICE g_aServices[] =
{
{
"Global",
NULL,
}
#ifdef RT_OS_WINDOWS
,
{
"Grant",
NULL,
}
#endif
};
/**
* Instantiates and starts the services.
*
* @returns VBox status code. Done bitching on failure.
*/
int supSvcCreateAndStartServices(void)
{
/*
* Validate that all services are in the NotCreated state.
*/
unsigned i;
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
{
supSvcLogError("service %s in state %d, expected state %d (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;
if (RT_FAILURE(rc))
{
break;
}
}
if (RT_SUCCESS(rc))
{
for (i = 0; i < RT_ELEMENTS(g_aServices); i++)
{
}
}
else
{
/*
* Destroy any services we managed to instantiate.
*/
while (i-- > 0)
{
}
}
return rc;
}
/**
* Checks if it's possible to stop the services.
*
* @returns VBox status code, done bitching on failure.
*/
int supSvcTryStopServices(void)
{
/*
* 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++)
cRunning++;
{
supSvcLogError("service %s in state %d (NotCreated), expected pause or running",
return VERR_WRONG_ORDER;
}
if (!cRunning)
return VINF_SUCCESS; /* all stopped, nothing to do. */
/*
* Try stop them in reverse of start order.
*/
int rc = VINF_SUCCESS;
i = RT_ELEMENTS(g_aServices);
while (i-- > 0)
{
if (RT_FAILURE(rc))
{
break;
}
}
if (RT_FAILURE(rc))
{
/* Failed, restart the ones we succeeded in stopping. */
while (++i < RT_ELEMENTS(g_aServices))
{
}
}
return rc;
}
/**
* Stops and destroys the services.
*/
void supSvcStopAndDestroyServices(void)
{
/*
* Stop and destroy the service in reverse of start order.
*/
unsigned i = RT_ELEMENTS(g_aServices);
while (i-- > 0)
{
}
}
/**
* 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)
{
}
/**
* 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).
*/
{
if (*pszFormat)
{
{
}
else
}
}
/**
* 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, ...)
{
}
/**
* 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",
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.
*/
{
return 1;
}
/**
* Prints an error message to the screen.
*
* @param pszFormat The message format string.
* @param va Format arguments.
*/
{
}
/**
* Prints an error message to the screen.
*
* @param pszFormat The message format string.
* @param ... Format arguments.
*/
void supSvcDisplayError(const char *pszFormat, ...)
{
}
/**
* 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",
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.
*/
{
return 1;
}