VBoxService.cpp revision b6517c5cc3b7a38889416706905a3cf2fd010785
/* $Id$ */
/** @file
* VBoxService - Guest Additions Service Skeleton.
*/
/*
* Copyright (C) 2007-2010 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.
*/
/*******************************************************************************
* Header Files *
*******************************************************************************/
/** @todo LOG_GROUP*/
#ifndef _MSC_VER
# include <unistd.h>
#endif
#include <errno.h>
#ifndef RT_OS_WINDOWS
# include <signal.h>
#endif
#include "product-generated.h"
#include <iprt/buildconfig.h>
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxServiceInternal.h"
/*******************************************************************************
* Global Variables *
*******************************************************************************/
/** The program name (derived from argv[0]). */
char *g_pszProgName = (char *)"";
/** The current verbosity level. */
int g_cVerbosity = 0;
/** The default service interval (the -i | --interval) option). */
#ifdef RT_OS_WINDOWS
/** Signal shutdown to the Windows service thread. */
static bool volatile g_fWindowsServiceShutdown;
/** Event the Windows service thread waits for shutdown. */
static RTSEMEVENT g_hEvtWindowsService;
#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
#ifdef VBOXSERVICE_VMINFO
{ &g_VMInfo, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOXSERVICE_CPUHOTPLUG
{ &g_CpuHotPlug, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOXSERVICE_MANAGEMENT
# ifdef VBOX_WITH_MEMBALLOON
{ &g_MemBalloon, NIL_RTTHREAD, false, false, false, true },
# endif
{ &g_VMStatistics, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOX_WITH_PAGE_SHARING
{ &g_PageSharing, NIL_RTTHREAD, false, false, false, true },
#endif
#ifdef VBOX_WITH_SHARED_FOLDERS
{ &g_AutoMount, NIL_RTTHREAD, false, false, false, true },
#endif
};
/**
* Displays the program usage message.
*
* @returns 1.
*/
static int VBoxServiceUsage(void)
{
RTPrintf("Usage:\n"
" %-12s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
" [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
#ifdef RT_OS_WINDOWS
RTPrintf(" [-r|--register] [-u|--unregister]\n");
#endif
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
RTPrintf("\n"
"Options:\n"
" -i | --interval The default interval.\n"
" -f | --foreground Don't daemonzie the program. For debugging.\n"
" -v | --verbose Increment the verbosity level. For debugging.\n"
" -h | -? | --help Show this message and exit with status 1.\n"
);
#ifdef RT_OS_WINDOWS
RTPrintf(" -r | --register Installs the service.\n"
" -u | --unregister Uninstall service.\n");
#endif
RTPrintf("\n"
"Service-specific options:\n");
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
RTPrintf(" --enable-%-14s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
RTPrintf(" --disable-%-13s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
}
RTPrintf("\n"
return 1;
}
/**
* Displays a syntax error message.
*
* @returns RTEXITCODE_SYNTAX.
* @param pszFormat The message text.
* @param ... Format arguments.
*/
{
return RTEXITCODE_SYNTAX;
}
/**
* Displays an error message.
*
* @returns RTEXITCODE_FAILURE.
* @param pszFormat The message text.
* @param ... Format arguments.
*/
{
return RTEXITCODE_FAILURE;
}
/**
* 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.
*/
{
#ifndef RT_OS_WINDOWS
/*
* Block all signals for this thread. Only the main thread will handle signals.
*/
#endif
return rc;
}
/**
* Check if at least one service should be started.
*/
static bool VBoxServiceCheckStartedServices(void)
{
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fEnabled)
return true;
return false;
}
/**
* Starts the service.
*
* @returns VBox status code, errors are fully bitched.
*/
int VBoxServiceStartServices(void)
{
int rc;
/*
* Initialize the services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fEnabled)
{
if (RT_FAILURE(rc))
{
if (rc != VERR_SERVICE_DISABLED)
{
VBoxServiceError("Service '%s' failed to initialize: %Rrc\n",
return rc;
}
g_aServices[j].fEnabled = false;
VBoxServiceVerbose(0, "Service '%s' was disabled because of missing functionality\n",
}
}
/*
* Start the service(s).
*/
rc = VINF_SUCCESS;
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (!g_aServices[j].fEnabled)
continue;
if (RT_FAILURE(rc))
{
break;
}
g_aServices[j].fStarted = true;
/* wait for the thread to initialize */
if (g_aServices[j].fShutdown)
{
}
}
return rc;
}
/**
* Stops and terminates the services.
*
* This should be called even when VBoxServiceStartServices fails so it can
* clean up anything that we succeeded in starting.
*/
int VBoxServiceStopServices(void)
{
int rc = VINF_SUCCESS;
/*
* Signal all the services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
/*
* Do the pfnStop callback on all running services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fStarted)
{
VBoxServiceVerbose(3, "Calling stop function for service '%s' ...\n", g_aServices[j].pDesc->pszName);
}
/*
* Wait for all the service threads to complete.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
continue;
{
for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
{
if (RT_SUCCESS(rc))
break;
#ifdef RT_OS_WINDOWS
/* Notify SCM that it takes a bit longer ... */
VBoxServiceWinSetStopPendingStatus(i + j*32);
#endif
}
if (RT_FAILURE(rc))
}
}
#ifdef RT_OS_WINDOWS
/*
* Wake up and tell the main() thread that we're shutting down (it's
* sleeping in VBoxServiceMainWait).
*/
if (g_hEvtWindowsService != NIL_RTSEMEVENT)
{
}
#endif
return rc;
}
/**
* Block the main thread until the service shuts down.
*/
void VBoxServiceMainWait(void)
{
int rc;
/* Report the host that we're up and running! */
0); /* Flags; not used. */
if (RT_FAILURE(rc))
#ifdef RT_OS_WINDOWS
/*
* Wait for the semaphore to be signalled.
*/
while (!ASMAtomicReadBool(&g_fWindowsServiceShutdown))
{
}
#else
/*
* Wait explicitly for a HUP, INT, QUIT, ABRT or TERM signal, blocking
* all important signals.
*
* sigwait returns when we receive a SIGCHLD. Kind of makes sense since
*/
int iSignal;
do
{
iSignal = -1;
}
# ifdef ERESTART
# endif
);
#endif /* !RT_OS_WINDOWS */
}
{
/*
* Init globals and such.
*/
RTR3Init();
/*
* Connect to the kernel part before daemonizing so we can fail and
* complain if there is some kind of problem. We need to initialize the
* guest lib *before* we do the pre-init just in case one of services needs
* do to some initial stuff with it.
*/
int rc = VbglR3Init();
if (RT_FAILURE(rc))
#ifdef RT_OS_WINDOWS
/*
* Check if we're the specially spawned VBoxService.exe process that
* handles page fusion. This saves an extra executable.
*/
if ( argc == 2
return VBoxServicePageSharingInitFork();
#endif
/*
* Do pre-init of services.
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (RT_FAILURE(rc))
}
#ifdef RT_OS_WINDOWS
/*
* Make sure only one instance of VBoxService runs at a time. Create a
* global mutex for that. Do not use a global namespace ("Global\\") for
* mutex name here, will blow up NT4 compatibility!
*/
/** @todo r=bird: Use Global\\ prefix or this serves no purpose on terminal servers. */
if ( hMutexAppRunning != NULL
&& GetLastError() == ERROR_ALREADY_EXISTS)
{
/* Close the mutex for this application instance. */
/** @todo r=bird: How does this cause us to terminate? Btw. Why do
* we do this before parsing parameters? 'VBoxService --help'
* and 'VBoxService --version' won't work now when the service
* is running... */
}
#endif
/*
* Parse the arguments.
*/
bool fDaemonize = true;
bool fDaemonized = 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";
#ifdef RT_OS_WINDOWS
else if (MATCHES("register"))
psz = "r";
else if (MATCHES("unregister"))
psz = "u";
#endif
else if (MATCHES("daemonized"))
{
fDaemonized = true;
continue;
}
else
{
bool fFound = false;
g_aServices[j].fEnabled = true;
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':
case '?':
return VBoxServiceUsage();
#ifdef RT_OS_WINDOWS
case 'r':
return VBoxServiceWinInstall();
case 'u':
return VBoxServiceWinUninstall();
#endif
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.
*/
if (!VBoxServiceCheckStartedServices())
return VBoxServiceSyntax("At least one service must be enabled.\n");
VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
/*
* Daemonize if requested.
*/
if (fDaemonize && !fDaemonized)
{
#ifdef RT_OS_WINDOWS
#else
if (RT_FAILURE(rc))
/* in-child */
#endif
}
#ifdef RT_OS_WINDOWS
else
#endif
{
/*
* Windows: We're running the service as a console application now. Start the
* services, enter the main thread's run loop and stop them again
* when it returns.
*
* POSIX: This is used for both daemons and console runs. Start all services
* and return immediately.
*/
if (RT_SUCCESS(rc))
}
#ifdef RT_OS_WINDOWS
/*
* Release instance mutex if we got it.
*/
if (hMutexAppRunning != NULL)
{
}
#endif
VBoxServiceVerbose(0, "Ended.\n");
return rcExit;
}