VBoxService.cpp revision 4a28f3bdba4c7d3b98bbf366d146a3850eb2bd44
/** $Id$ */
/** @file
* VBoxService - Guest Additions Service Skeleton.
*/
/*
* Copyright (C) 2007 innotek GmbH
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include <errno.h>
#include <iprt/initterm.h>
#include <VBox/VBoxGuest.h>
#include "VBoxServiceInternal.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The program name (derived from argv[0]). */
char *g_pszProgName = "";
/** The current verbosity level. */
int g_cVerbosity = 0;
/** The default service interval (the -i | --interval) option). */
/** Shutdown the main thread. (later, for signals) */
bool volatile g_fShutdown;
#ifndef RT_OS_OS2
#endif
/**
* The details of the services that has been compiled in.
*/
static struct
{
/** Pointer to the service descriptor. */
/** The worker thread. NIL_RTTHREAD if it's the main thread. */
/** Shutdown indicator. */
bool volatile fShutdown;
/** Indicator set by the service thread exiting. */
bool volatile fStopped;
/** Whether the service was started or not. */
bool fStarted;
/** Whether the service is enabled or not. */
bool fEnabled;
} g_aServices[] =
{
#ifdef VBOXSERVICE_CONTROL
{ &g_Control, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOXSERVICE_TIMESYNC
{ &g_TimeSync, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOXSERVICE_CLIPBOARD
{ &g_Clipboard, NIL_RTTHREAD, false, false, false, true },
#endif
};
/**
* Displays the program usage message.
*
* @returns 1.
*/
static int VBoxServiceUsage(void)
{
RTPrintf("usage: %s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
" [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
RTPrintf("\n"
"Options:\n"
" -f | --foreground Don't daemonzie the program. For debugging.\n"
" -v | --verbose Increment the verbosity level. For debugging.\n"
" -i | --interval The default interval.\n"
" -h | -? | --help Show this message and exit with status 1.\n");
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
RTPrintf(" --enable-%-10s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
RTPrintf(" --disable-%-9s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
}
RTPrintf("\n"
" Copyright (C) 2007 innotek GmbH\n");
return 1;
}
/**
* Displays a syntax error message.
*
* @returns 1
* @param pszFormat The message text.
* @param ... Format arguments.
*/
int VBoxServiceSyntax(const char *pszFormat, ...)
{
return 1;
}
/**
* Displays an error message.
*
* @returns 1
* @param pszFormat The message text.
* @param ... Format arguments.
*/
int VBoxServiceError(const char *pszFormat, ...)
{
return 1;
}
/**
* Displays a verbose message.
*
* @returns 1
* @param pszFormat The message text.
* @param ... Format arguments.
*/
{
if (iLevel <= g_cVerbosity)
{
}
}
/**
* Gets a 32-bit value argument.
*
* @returns 0 on success, non-zero exit code on error.
* @param argc The argument count.
* @param argv The argument vector
* @param psz Where in *pi to start looking for the value argument.
* @param pi Where to find and perhaps update the argument index.
* @param pu32 Where to store the 32-bit value.
* @param u32Min The minimum value.
* @param u32Max The maximum value.
*/
int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
{
psz++;
if (!*psz)
{
}
char *pszNext;
return VBoxServiceSyntax("The timesync interval of %RU32 secconds is out of range [%RU32..%RU32].\n",
return 0;
}
/**
* The service thread.
*
* @returns Whatever the worker function returns.
* @param ThreadSelf My thread handle.
* @param pvUser The service index.
*/
{
return rc;
}
{
int rc;
/*
* Init globals and such.
*/
RTR3Init(false, 0);
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (RT_FAILURE(rc))
}
/*
* Parse the arguments.
*/
bool fDaemonize = true;
bool fDaemonzied = false;
for (int i = 1; i < argc; i++)
{
if (*psz != '-')
psz++;
/* translate long argument to short */
if (*psz == '-')
{
psz++;
if (MATCHES("foreground"))
psz = "f";
else if (MATCHES("verbose"))
psz = "v";
else if (MATCHES("help"))
psz = "h";
else if (MATCHES("interval"))
psz = "i";
else if (MATCHES("daemonized"))
{
fDaemonzied = true;
continue;
}
else
{
bool fFound = false;
#if defined(RT_OS_OS2)
#else
#endif
g_aServices[j].fEnabled = true;
#if defined(RT_OS_OS2)
#else
#endif
g_aServices[j].fEnabled = false;
if (!fFound)
{
if (fFound)
break;
if (rc != -1)
return rc;
}
if (!fFound)
continue;
}
}
/* handle the string of short options. */
do
{
switch (*psz)
{
case 'i':
if (rc)
return rc;
break;
case 'f':
fDaemonize = false;
break;
case 'v':
g_cVerbosity++;
break;
case 'h':
return VBoxServiceUsage();
default:
{
bool fFound = false;
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (fFound)
break;
if (rc != -1)
return rc;
}
if (!fFound)
break;
}
}
}
/*
* Check that at least one service is enabled.
*/
unsigned iMain = ~0U;
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fEnabled)
{
iMain = j;
break;
}
if (iMain == ~0U)
return VBoxServiceSyntax("At least one service must be enabled.\n");
/*
* Connect to the kernel part before daemonizing so we can fail
* and complain if there is some kind of problem.
*/
rc = VbglR3Init();
if (RT_FAILURE(rc))
/*
* Daemonize if requested.
*/
if (fDaemonize && !fDaemonzied)
{
errno = 0;
if (daemon(0, 0) != 0)
/* in-child */
}
/** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
/*
* Initialize the services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (RT_FAILURE(rc))
}
/*
* Start the service(s).
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if ( !g_aServices[j].fEnabled
|| j == iMain)
continue;
if (RT_FAILURE(rc))
{
break;
}
g_aServices[j].fStarted = true;
/* wait for the thread to initialize */
if (g_aServices[j].fShutdown)
}
if (RT_SUCCESS(rc))
{
/* The final service runs in the main thread. */
VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
}
/*
* Stop and terminate the services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fStarted)
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
{
if (RT_FAILURE(rc))
}
}
return 0;
}