VBoxAutostart.cpp revision 73bef7f249418907cb1dc8dce51319772a3dbeee
/* $Id$ */
/** @file
* VBoxAutostart - VirtualBox Autostart service.
*/
/*
* Copyright (C) 2012 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 *
*******************************************************************************/
#include <package-generated.h>
#include <iprt/buildconfig.h>
#include <iprt/critsect.h>
#include <iprt/initterm.h>
#include <iprt/semaphore.h>
#include <algorithm>
#include <list>
#include <string>
#include <signal.h>
using namespace com;
/**
* VM list entry.
*/
typedef struct AUTOSTARTVM
{
} AUTOSTARTVM;
static bool g_fVerbose = false;
/** Logging parameters. */
/** Run in background. */
static bool g_fDaemonize = false;
/**
* Command line arguments.
*/
static const RTGETOPTDEF g_aOptions[] = {
#endif
/** For displayHelp(). */
};
static void serviceLog(const char *pszFormat, ...)
{
}
{
}
/**
* Main routine for the autostart daemon.
*
* @returns exit status code.
*/
static RTEXITCODE autostartMain(void)
{
/*
* Build a list of all VMs we need to autostart first, apply the overrides
* from the configuration and start the VMs afterwards.
*/
{
/*
* Iterate through the collection
*/
{
if (machines[i])
{
if (!fAccessible)
continue;
if (fAutostart)
{
}
}
}
{
/* Sort by startup delay and apply base override. */
{
machine.asOutParam()));
{
{
{
{
{
}
else
{
}
}
}
}
}
}
}
}
return rcExit;
}
static void displayHeader()
{
"All rights reserved.\n\n");
}
/**
* Displays the help.
*
* @param pszImage Name of program name (image).
*/
static void displayHelp(const char *pszImage)
{
"Usage:\n"
" %s [-v|--verbose] [-h|-?|--help]\n"
" [-F|--logfile=<file>] [-R|--logrotate=<num>] [-S|--logsize=<bytes>]\n"
" [-I|--loginterval=<seconds>]\n"
" [-c|--config=<config file>]\n", pszImage);
"Options:\n");
for (unsigned i = 0;
i < RT_ELEMENTS(g_aOptions);
++i)
{
{
str += ", -";
}
str += ":";
const char *pcszDescr = "";
switch (g_aOptions[i].iShort)
{
case 'h':
pcszDescr = "Print this help message and exit.";
break;
case 'b':
pcszDescr = "Run in background (daemon mode).";
break;
#endif
case 'P':
pcszDescr = "Name of the PID file which is created when the daemon was started.";
break;
case 'F':
pcszDescr = "Name of file to write log to (no file).";
break;
case 'R':
pcszDescr = "Number of log files (0 disables log rotation).";
break;
case 'S':
pcszDescr = "Maximum size of a log file to trigger rotation (bytes).";
break;
case 'I':
pcszDescr = "Maximum time interval to trigger log rotation (seconds).";
break;
case 'c':
pcszDescr = "Name of the configuration file for the global overrides.";
break;
}
}
RTStrmPrintf(g_pStdErr, "\nUse environment variable VBOXAUTOSTART_RELEASE_LOG for logging options.\n");
}
/**
* Creates all global COM objects.
*
* @return HRESULT
*/
static int autostartSetup()
{
serviceLogVerbose(("Setting up ...\n"));
/*
* Setup VirtualBox + session interfaces.
*/
{
}
else
return VERR_COM_OBJECT_NOT_FOUND;
return VINF_SUCCESS;
}
static void autostartShutdown()
{
serviceLogVerbose(("Shutting down ...\n"));
}
{
/*
* Before we do anything, init the runtime without loading
* the support driver.
*/
if (RT_FAILURE(rc))
return RTMsgInitFailure(rc);
/*
* Parse the global options
*/
int c;
const char *pszLogFile = NULL;
const char *pszConfigFile = NULL;
bool fQuiet = false;
bool fStart = false;
bool fStop = false;
{
switch (c)
{
case 'h':
displayHelp(argv[0]);
return 0;
case 'v':
g_fVerbose = true;
break;
case 'b':
g_fDaemonize = true;
break;
#endif
case 'V':
return 0;
case 'P':
break;
case 'F':
break;
case 'R':
break;
case 'S':
break;
case 'I':
break;
case 'Q':
fQuiet = true;
break;
case 'c':
break;
case 's':
fStart = true;
break;
case 'd':
fStop = true;
break;
default:
return RTGetOptPrintError(c, &ValueUnion);
}
}
{
displayHelp(argv[0]);
}
if (!fQuiet)
/* create release logger, to stdout */
"all", "VBOXAUTOSTART_RELEASE_LOG",
if (RT_FAILURE(rc))
if (g_fDaemonize)
{
/* prepare release logging */
char szLogFile[RTPATH_MAX];
if (!pszLogFile || !*pszLogFile)
{
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
}
if (RT_FAILURE(rc))
/* create release logger, to file */
"all", "VBOXAUTOSTART_RELEASE_LOG",
if (RT_FAILURE(rc))
}
#endif
/*
* Initialize COM.
*/
using namespace com;
# ifdef VBOX_WITH_XPCOM
if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
{
return RTMsgErrorExit(RTEXITCODE_FAILURE,
"Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
}
# endif
{
{
RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
}
else
return RTEXITCODE_FAILURE;
}
rc = autostartSetup();
if (RT_FAILURE(rc))
return RTEXITCODE_FAILURE;
return rcExit;
}