VBoxService.cpp revision e60b397fc7f34482960cc63f63db84d6a02226d1
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** $Id$ */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** @file
5b281ba489ca18f0380d7efc7a5108b606cce449vboxsync * VBoxService - Guest Additions Service Skeleton.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Copyright (C) 2007 innotek GmbH
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * This file is part of VirtualBox Open Source Edition (OSE), as
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * available from http://www.virtualbox.org. This file is free software;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * you can redistribute it and/or modify it under the terms of the GNU
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * General Public License (GPL) as published by the Free Software
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Foundation, in version 2 as it comes in the "COPYING" file of the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Header Files *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#ifndef _MSC_VER
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync# include <unistd.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#endif
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <errno.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/thread.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/string.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/stream.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/initterm.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/asm.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <iprt/path.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include <VBox/VBoxGuest.h>
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#include "VBoxServiceInternal.h"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/*******************************************************************************
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync* Global Variables *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync*******************************************************************************/
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** The program name (derived from argv[0]). */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncchar *g_pszProgName = "";
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** The current verbosity level. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncint g_cVerbosity = 0;
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsync/** The default service interval (the -i | --interval) option). */
881b5ff6bc55e1fb0f4ef42f9782ccec79c0a138vboxsyncuint32_t g_DefaultInterval = 0;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/** Shutdown the main thread. (later, for signals) */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncbool volatile g_fShutdown;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * The details of the services that has been compiled in.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic struct
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Pointer to the service descriptor. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync PCVBOXSERVICE pDesc;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** The worker thread. NIL_RTTHREAD if it's the main thread. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTTHREAD Thread;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Shutdown indicator. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool volatile fShutdown;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Indicator set by the service thread exiting. */
9621896680fea9b2078823e8ef2e64cec5bf2da0vboxsync bool volatile fStopped;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /** Whether the service was started or not. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool fStarted;
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /** Whether the service is enabled or not. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool fEnabled;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync} g_aServices[] =
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#ifdef VBOXSERVICE_CONTROL
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync { &g_Control, NIL_RTTHREAD, false, false, false, true },
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#endif
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#ifdef VBOXSERVICE_TIMESYNC
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync { &g_TimeSync, NIL_RTTHREAD, false, false, false, true },
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#endif
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#ifdef VBOXSERVICE_CLIPBOARD
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync { &g_Clipboard, NIL_RTTHREAD, false, false, false, true },
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#endif
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync};
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Displays the program usage message.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns 1.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic int VBoxServiceUsage(void)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf("usage: %s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf(" %s\n", g_aServices[j].pDesc->pszUsage);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf("\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync "Options:\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " -f | --foreground Don't daemonzie the program. For debugging.\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " -v | --verbose Increment the verbosity level. For debugging.\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " -i | --interval The default interval.\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " -h | -? | --help Show this message and exit with status 1.\n");
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf(" --enable-%-10s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf(" --disable-%-9s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf("%s", g_aServices[j].pDesc->pszOptions);
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTPrintf("\n"
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync " Copyright (C) 2008 Sun Microsystems, Inc.\n");
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
590bfe12ce22cd3716448fbb9f4dc51664bfe5e2vboxsync return 1;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Displays a syntax error message.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns 1
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsync * @param pszFormat The message text.
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsync * @param ... Format arguments.
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsync */
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsyncint VBoxServiceSyntax(const char *pszFormat, ...)
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsync{
3f1eb07aba9eee6394f3084c956149ee9a27df09vboxsync RTStrmPrintf(g_pStdErr, "%s: syntax error: ", g_pszProgName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_list va;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_start(va, pszFormat);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTStrmPrintfV(g_pStdErr, pszFormat, va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_end(va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync return 1;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Displays an error message.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns 1
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pszFormat The message text.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param ... Format arguments.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncint VBoxServiceError(const char *pszFormat, ...)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_list va;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_start(va, pszFormat);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTStrmPrintfV(g_pStdErr, pszFormat, va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_end(va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return 1;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Displays a verbose message.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync * @returns 1
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pszFormat The message text.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param ... Format arguments.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncvoid VBoxServiceVerbose(int iLevel, const char *pszFormat, ...)
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (iLevel <= g_cVerbosity)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTStrmPrintf(g_pStdOut, "%s: ", g_pszProgName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_list va;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_start(va, pszFormat);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTStrmPrintfV(g_pStdOut, pszFormat, va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync va_end(va);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Gets a 32-bit value argument.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns 0 on success, non-zero exit code on error.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param argc The argument count.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param argv The argument vector
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param psz Where in *pi to start looking for the value argument.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pi Where to find and perhaps update the argument index.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pu32 Where to store the 32-bit value.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param u32Min The minimum value.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param u32Max The maximum value.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncint VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (*psz == ':' || *psz == '=')
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz++;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (!*psz)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (*pi + 1 >= argc)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("Missing value for the '%s' argument\n", argv[*pi]);
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync psz = argv[++*pi];
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync char *pszNext;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (RT_FAILURE(rc) || *pszNext)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("Failed to convert interval '%s' to a number.\n", psz);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (*pu32 < u32Min || *pu32 > u32Max)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("The timesync interval of %RU32 secconds is out of range [%RU32..%RU32].\n",
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *pu32, u32Min, u32Max);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return 0;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync/**
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * The service thread.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync *
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @returns Whatever the worker function returns.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param ThreadSelf My thread handle.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * @param pvUser The service index.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncstatic DECLCALLBACK(int) VBoxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync const unsigned i = (uintptr_t)pvUser;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTThreadUserSignal(ThreadSelf);
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync return rc;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync}
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsyncint main(int argc, char **argv)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync{
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync int rc;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Init globals and such.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync RTR3Init(false, 0);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync g_pszProgName = RTPathFilename(argv[0]);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = g_aServices[j].pDesc->pfnPreInit();
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (RT_FAILURE(rc))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
ad27e1d5e48ca41245120c331cc88b50464813cevboxsync /*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Parse the arguments.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool fDaemonize = true;
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync bool fDaemonzied = false;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (int i = 1; i < argc; i++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync const char *psz = argv[i];
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (*psz != '-')
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz++;
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* translate long argument to short */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (*psz == '-')
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz++;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync size_t cch = strlen(psz);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync && !memcmp(psz, strconst, sizeof(strconst) - 1) )
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (MATCHES("foreground"))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz = "f";
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync else if (MATCHES("verbose"))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz = "v";
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync else if (MATCHES("help"))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz = "h";
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync else if (MATCHES("interval"))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz = "i";
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync else if (MATCHES("daemonized"))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync fDaemonzied = true;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync continue;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync else
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool fFound = false;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync g_aServices[j].fEnabled = true;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync g_aServices[j].fEnabled = false;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (!fFound)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync fFound = rc == 0;
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync if (fFound)
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync break;
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync if (rc != -1)
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync return rc;
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync }
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync if (!fFound)
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
0cde281779e97ead3181bbd3b628451fa2b1efe1vboxsync continue;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync#undef MATCHES
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* handle the string of short options. */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync do
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync switch (*psz)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync case 'i':
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (rc)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return rc;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync psz = NULL;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync case 'f':
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync fDaemonize = false;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync case 'v':
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync g_cVerbosity++;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync case 'h':
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceUsage();
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync default:
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync bool fFound = false;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync fFound = rc == 0;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (fFound)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (rc != -1)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return rc;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (!fFound)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync }
0f77dc54d7ec617480988ccdfcd080f480e79698vboxsync } while (psz && *++psz);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Check that at least one service is enabled.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync unsigned iMain = ~0U;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (g_aServices[j].fEnabled)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync iMain = j;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync break;
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (iMain == ~0U)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceSyntax("At least one service must be enabled.\n");
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
f0ed7ab5e7f8d2f73b5aa08e46eb3a04cbb31cb2vboxsync /*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Connect to the kernel part before daemonizing so we can fail
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * and complain if there is some kind of problem.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = VbglR3Init();
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (RT_FAILURE(rc))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /*
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync * Daemonize if requested.
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (fDaemonize && !fDaemonzied)
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync {
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync VBoxServiceVerbose(1, "Daemonizing...\n");
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync if (RT_FAILURE(rc))
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync return VBoxServiceError("daemon failed: %Rrc\n", rc);
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync /* in-child */
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync }
d1c5a03c19683c719b94496bb998fde2f2e5e622vboxsync
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync/** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync /*
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync * Initialize the services.
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync */
0dfc79e0666da4c8853deda18a14ebf5819d0d78vboxsync for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
rc = g_aServices[j].pDesc->pfnInit();
if (RT_FAILURE(rc))
return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
}
/*
* Start the service(s).
*/
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if ( !g_aServices[j].fEnabled
|| j == iMain)
continue;
rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
if (RT_FAILURE(rc))
{
VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
break;
}
g_aServices[j].fStarted = true;
/* wait for the thread to initialize */
RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
if (g_aServices[j].fShutdown)
rc = VERR_GENERAL_FAILURE;
}
if (RT_SUCCESS(rc))
{
/* The final service runs in the main thread. */
VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
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++)
ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
if (g_aServices[j].fStarted)
g_aServices[j].pDesc->pfnStop();
for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
{
if (g_aServices[j].Thread != NIL_RTTHREAD)
{
rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
if (RT_FAILURE(rc))
VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
}
g_aServices[j].pDesc->pfnTerm();
}
return 0;
}