summaryrefslogtreecommitdiffstats
path: root/src/VBox/Frontends/VBoxAutostart
diff options
context:
space:
mode:
Diffstat (limited to 'src/VBox/Frontends/VBoxAutostart')
-rw-r--r--src/VBox/Frontends/VBoxAutostart/Makefile.kmk54
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostart-posix.cpp579
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp1493
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h383
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostartCfg.cpp748
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp222
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp267
-rw-r--r--src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp341
8 files changed, 4087 insertions, 0 deletions
diff --git a/src/VBox/Frontends/VBoxAutostart/Makefile.kmk b/src/VBox/Frontends/VBoxAutostart/Makefile.kmk
new file mode 100644
index 00000000..ce1cf253
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/Makefile.kmk
@@ -0,0 +1,54 @@
+# $Id: Makefile.kmk $
+## @file
+# VBoxAutostart - VM autostart service.
+#
+
+#
+# Copyright (C) 2012-2023 Oracle and/or its affiliates.
+#
+# This file is part of VirtualBox base platform packages, as
+# available from https://www.virtualbox.org.
+#
+# This program is free software; you can redistribute it and/or
+# modify it under the terms of the GNU General Public License
+# as published by the Free Software Foundation, in version 3 of the
+# License.
+#
+# This program is distributed in the hope that it will be useful, but
+# WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+# General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with this program; if not, see <https://www.gnu.org/licenses>.
+#
+# SPDX-License-Identifier: GPL-3.0-only
+#
+
+SUB_DEPTH = ../../../..
+include $(KBUILD_PATH)/subheader.kmk
+
+ifeq ($(KBUILD_TARGET),win)
+ PROGRAMS += VBoxAutostartSvc
+ VBoxAutostartSvc_TEMPLATE = VBoxMainClientExe
+ VBoxAutostartSvc_INCS = ../Common
+ VBoxAutostartSvc_SOURCES = \
+ VBoxAutostartCfg.cpp \
+ VBoxAutostartStart.cpp \
+ VBoxAutostartStop.cpp \
+ VBoxAutostartUtils.cpp \
+ VBoxAutostart-win.cpp \
+ ../Common/PasswordInput.cpp
+ VBoxAutostartSvc_LIBS.win += Secur32.lib
+else
+ PROGRAMS += VBoxAutostart
+ VBoxAutostart_TEMPLATE = VBoxMainClientExe
+ VBoxAutostart_SOURCES = \
+ VBoxAutostartCfg.cpp \
+ VBoxAutostartStart.cpp \
+ VBoxAutostartStop.cpp \
+ VBoxAutostartUtils.cpp \
+ VBoxAutostart-posix.cpp
+endif
+
+include $(FILE_KBUILD_SUB_FOOTER)
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-posix.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-posix.cpp
new file mode 100644
index 00000000..94f846db
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-posix.cpp
@@ -0,0 +1,579 @@
+/* $Id: VBoxAutostart-posix.cpp $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+
+/*********************************************************************************************************************************
+* Header Files *
+*********************************************************************************************************************************/
+#include <VBox/com/com.h>
+#include <VBox/com/string.h>
+#include <VBox/com/Guid.h>
+#include <VBox/com/array.h>
+#include <VBox/com/ErrorInfo.h>
+#include <VBox/com/errorprint.h>
+
+#include <VBox/com/NativeEventQueue.h>
+#include <VBox/com/listeners.h>
+#include <VBox/com/VirtualBox.h>
+
+#include <iprt/errcore.h>
+#include <VBox/log.h>
+#include <VBox/version.h>
+
+#include <package-generated.h>
+
+#include <iprt/asm.h>
+#include <iprt/buildconfig.h>
+#include <iprt/critsect.h>
+#include <iprt/getopt.h>
+#include <iprt/initterm.h>
+#include <iprt/message.h>
+#include <iprt/path.h>
+#include <iprt/process.h>
+#include <iprt/semaphore.h>
+#include <iprt/stream.h>
+#include <iprt/string.h>
+#include <iprt/system.h>
+#include <iprt/time.h>
+#include <iprt/ctype.h>
+#include <iprt/dir.h>
+#include <iprt/env.h>
+
+#include <signal.h>
+
+#include "VBoxAutostart.h"
+
+using namespace com;
+
+#if defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_DARWIN)
+# define VBOXAUTOSTART_DAEMONIZE
+#endif
+
+ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL;
+ComPtr<IVirtualBox> g_pVirtualBox = NULL;
+ComPtr<ISession> g_pSession = NULL;
+
+/** Logging parameters. */
+static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
+static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
+static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
+
+/** Verbosity level. */
+unsigned g_cVerbosity = 0;
+
+/** Run in background. */
+static bool g_fDaemonize = false;
+
+/**
+ * Command line arguments.
+ */
+static const RTGETOPTDEF g_aOptions[] = {
+#ifdef VBOXAUTOSTART_DAEMONIZE
+ { "--background", 'b', RTGETOPT_REQ_NOTHING },
+#endif
+ /** For displayHelp(). */
+ { "--help", 'h', RTGETOPT_REQ_NOTHING },
+ { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
+ { "--start", 's', RTGETOPT_REQ_NOTHING },
+ { "--stop", 'd', RTGETOPT_REQ_NOTHING },
+ { "--config", 'c', RTGETOPT_REQ_STRING },
+ { "--logfile", 'F', RTGETOPT_REQ_STRING },
+ { "--logrotate", 'R', RTGETOPT_REQ_UINT32 },
+ { "--logsize", 'S', RTGETOPT_REQ_UINT64 },
+ { "--loginterval", 'I', RTGETOPT_REQ_UINT32 },
+ { "--quiet", 'Q', RTGETOPT_REQ_NOTHING }
+};
+
+/** Set by the signal handler. */
+static volatile bool g_fCanceled = false;
+
+
+/**
+ * Signal handler that sets g_fCanceled.
+ *
+ * This can be executed on any thread in the process, on Windows it may even be
+ * a thread dedicated to delivering this signal. Do not doing anything
+ * unnecessary here.
+ */
+static void showProgressSignalHandler(int iSignal)
+{
+ NOREF(iSignal);
+ ASMAtomicWriteBool(&g_fCanceled, true);
+}
+
+/**
+ * Print out progress on the console.
+ *
+ * This runs the main event queue every now and then to prevent piling up
+ * unhandled things (which doesn't cause real problems, just makes things
+ * react a little slower than in the ideal case).
+ */
+DECLHIDDEN(HRESULT) showProgress(ComPtr<IProgress> progress)
+{
+ using namespace com;
+
+ BOOL fCompleted = FALSE;
+ ULONG ulCurrentPercent = 0;
+ ULONG ulLastPercent = 0;
+
+ Bstr bstrOperationDescription;
+
+ NativeEventQueue::getMainEventQueue()->processEventQueue(0);
+
+ ULONG cOperations = 1;
+ HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
+ if (FAILED(hrc))
+ {
+ RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
+ RTStrmFlush(g_pStdErr);
+ return hrc;
+ }
+
+ /*
+ * Note: Outputting the progress info to stderr (g_pStdErr) is intentional
+ * to not get intermixed with other (raw) stdout data which might get
+ * written in the meanwhile.
+ */
+ RTStrmPrintf(g_pStdErr, "0%%...");
+ RTStrmFlush(g_pStdErr);
+
+ /* setup signal handling if cancelable */
+ bool fCanceledAlready = false;
+ BOOL fCancelable;
+ hrc = progress->COMGETTER(Cancelable)(&fCancelable);
+ if (FAILED(hrc))
+ fCancelable = FALSE;
+ if (fCancelable)
+ {
+ signal(SIGINT, showProgressSignalHandler);
+#ifdef SIGBREAK
+ signal(SIGBREAK, showProgressSignalHandler);
+#endif
+ }
+
+ hrc = progress->COMGETTER(Completed(&fCompleted));
+ while (SUCCEEDED(hrc))
+ {
+ progress->COMGETTER(Percent(&ulCurrentPercent));
+
+ /* did we cross a 10% mark? */
+ if (ulCurrentPercent / 10 > ulLastPercent / 10)
+ {
+ /* make sure to also print out missed steps */
+ for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
+ {
+ if (curVal < 100)
+ {
+ RTStrmPrintf(g_pStdErr, "%u%%...", curVal);
+ RTStrmFlush(g_pStdErr);
+ }
+ }
+ ulLastPercent = (ulCurrentPercent / 10) * 10;
+ }
+
+ if (fCompleted)
+ break;
+
+ /* process async cancelation */
+ if (g_fCanceled && !fCanceledAlready)
+ {
+ hrc = progress->Cancel();
+ if (SUCCEEDED(hrc))
+ fCanceledAlready = true;
+ else
+ g_fCanceled = false;
+ }
+
+ /* make sure the loop is not too tight */
+ progress->WaitForCompletion(100);
+
+ NativeEventQueue::getMainEventQueue()->processEventQueue(0);
+ hrc = progress->COMGETTER(Completed(&fCompleted));
+ }
+
+ /* undo signal handling */
+ if (fCancelable)
+ {
+ signal(SIGINT, SIG_DFL);
+#ifdef SIGBREAK
+ signal(SIGBREAK, SIG_DFL);
+#endif
+ }
+
+ /* complete the line. */
+ LONG iRc = E_FAIL;
+ hrc = progress->COMGETTER(ResultCode)(&iRc);
+ if (SUCCEEDED(hrc))
+ {
+ if (SUCCEEDED(iRc))
+ RTStrmPrintf(g_pStdErr, "100%%\n");
+ else if (g_fCanceled)
+ RTStrmPrintf(g_pStdErr, "CANCELED\n");
+ else
+ {
+ RTStrmPrintf(g_pStdErr, "\n");
+ RTStrmPrintf(g_pStdErr, "Progress state: %Rhrc\n", iRc);
+ }
+ hrc = iRc;
+ }
+ else
+ {
+ RTStrmPrintf(g_pStdErr, "\n");
+ RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
+ }
+ RTStrmFlush(g_pStdErr);
+ return hrc;
+}
+
+DECLHIDDEN(void) autostartSvcOsLogStr(const char *pszMsg, AUTOSTARTLOGTYPE enmLogType)
+{
+ if ( enmLogType == AUTOSTARTLOGTYPE_VERBOSE
+ && !g_cVerbosity)
+ return;
+
+ LogRel(("%s", pszMsg));
+}
+
+/**
+ * Shows the help.
+ *
+ * @param pszImage Name of program name (image).
+ */
+static void showHelp(const char *pszImage)
+{
+ AssertPtrReturnVoid(pszImage);
+
+ autostartSvcShowHeader();
+
+ RTStrmPrintf(g_pStdErr,
+ "Usage: %s [-v|--verbose] [-h|-?|--help]\n"
+ " [-V|--version]\n"
+ " [-F|--logfile=<file>] [-R|--logrotate=<num>]\n"
+ " [-S|--logsize=<bytes>] [-I|--loginterval=<seconds>]\n"
+ " [-c|--config=<config file>]\n",
+ pszImage);
+
+ RTStrmPrintf(g_pStdErr,
+ "\n"
+ "Options:\n");
+ for (unsigned i = 0; i < RT_ELEMENTS(g_aOptions); i++)
+ {
+ const char *pcszDescr;
+ switch (g_aOptions[i].iShort)
+ {
+ case 'h':
+ pcszDescr = "Prints this help message and exit.";
+ break;
+
+#ifdef VBOXAUTOSTART_DAEMONIZE
+ case 'b':
+ pcszDescr = "Run in background (daemon mode).";
+ break;
+#endif
+
+ 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;
+
+ case 'V':
+ pcszDescr = "Shows the service version.";
+ break;
+
+ default:
+ AssertFailedBreakStmt(pcszDescr = "");
+ }
+
+ if (g_aOptions[i].iShort < 1000)
+ RTStrmPrintf(g_pStdErr,
+ " %s, -%c\n"
+ " %s\n", g_aOptions[i].pszLong, g_aOptions[i].iShort, pcszDescr);
+ else
+ RTStrmPrintf(g_pStdErr,
+ " %s\n"
+ " %s\n", g_aOptions[i].pszLong, pcszDescr);
+ }
+
+ RTStrmPrintf(g_pStdErr,
+ "\n"
+ "Use environment variable VBOXAUTOSTART_RELEASE_LOG for logging options.\n");
+}
+
+int main(int argc, char *argv[])
+{
+ /*
+ * Before we do anything, init the runtime without loading
+ * the support driver.
+ */
+ int rc = RTR3InitExe(argc, &argv, 0);
+ 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;
+ RTGETOPTUNION ValueUnion;
+ RTGETOPTSTATE GetState;
+ RTGetOptInit(&GetState, argc, argv,
+ g_aOptions, RT_ELEMENTS(g_aOptions), 1 /* First */, 0 /*fFlags*/);
+ while ((c = RTGetOpt(&GetState, &ValueUnion)))
+ {
+ switch (c)
+ {
+ case 'h':
+ showHelp(argv[0]);
+ return RTEXITCODE_SUCCESS;
+
+ case 'v':
+ g_cVerbosity++;
+ break;
+
+#ifdef VBOXAUTOSTART_DAEMONIZE
+ case 'b':
+ g_fDaemonize = true;
+ break;
+#endif
+ case 'V':
+ autostartSvcShowVersion(false);
+ return RTEXITCODE_SUCCESS;
+
+ case 'F':
+ pszLogFile = ValueUnion.psz;
+ break;
+
+ case 'R':
+ g_cHistory = ValueUnion.u32;
+ break;
+
+ case 'S':
+ g_uHistoryFileSize = ValueUnion.u64;
+ break;
+
+ case 'I':
+ g_uHistoryFileTime = ValueUnion.u32;
+ break;
+
+ case 'Q':
+ fQuiet = true;
+ break;
+
+ case 'c':
+ pszConfigFile = ValueUnion.psz;
+ break;
+
+ case 's':
+ fStart = true;
+ break;
+
+ case 'd':
+ fStop = true;
+ break;
+
+ default:
+ return RTGetOptPrintError(c, &ValueUnion);
+ }
+ }
+
+ if (!fStart && !fStop)
+ {
+ showHelp(argv[0]);
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "Either --start or --stop must be present");
+ }
+ else if (fStart && fStop)
+ {
+ showHelp(argv[0]);
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "--start or --stop are mutually exclusive");
+ }
+
+ if (!pszConfigFile)
+ {
+ showHelp(argv[0]);
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "--config <config file> is missing");
+ }
+
+ if (!fQuiet)
+ autostartSvcShowHeader();
+
+ PCFGAST pCfgAst = NULL;
+ char *pszUser = NULL;
+ PCFGAST pCfgAstUser = NULL;
+ PCFGAST pCfgAstPolicy = NULL;
+ PCFGAST pCfgAstUserHome = NULL;
+ bool fAllow = false;
+
+ rc = autostartParseConfig(pszConfigFile, &pCfgAst);
+ if (RT_FAILURE(rc))
+ return RTEXITCODE_FAILURE;
+
+ rc = RTProcQueryUsernameA(RTProcSelf(), &pszUser);
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to query username of the process");
+
+ pCfgAstUser = autostartConfigAstGetByName(pCfgAst, pszUser);
+ pCfgAstPolicy = autostartConfigAstGetByName(pCfgAst, "default_policy");
+
+ /* Check default policy. */
+ if (pCfgAstPolicy)
+ {
+ if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
+ && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow")
+ || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "deny")))
+ {
+ if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow"))
+ fAllow = true;
+ }
+ else
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "'default_policy' must be either 'allow' or 'deny'");
+ }
+
+ if ( pCfgAstUser
+ && pCfgAstUser->enmType == CFGASTNODETYPE_COMPOUND)
+ {
+ pCfgAstPolicy = autostartConfigAstGetByName(pCfgAstUser, "allow");
+ if (pCfgAstPolicy)
+ {
+ if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
+ && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true")
+ || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "false")))
+ {
+ if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true"))
+ fAllow = true;
+ else
+ fAllow = false;
+ }
+ else
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "'allow' must be either 'true' or 'false'");
+ }
+ pCfgAstUserHome = autostartConfigAstGetByName(pCfgAstUser, "VBOX_USER_HOME");
+ if ( pCfgAstUserHome
+ && pCfgAstUserHome->enmType == CFGASTNODETYPE_KEYVALUE)
+ {
+ rc = RTEnvSet("VBOX_USER_HOME", pCfgAstUserHome->u.KeyValue.aszValue);
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "'VBOX_USER_HOME' could not be set for this user");
+ }
+ }
+ else if (pCfgAstUser)
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid config, user is not a compound node");
+
+ if (!fAllow)
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "User is not allowed to autostart VMs");
+
+ RTStrFree(pszUser);
+
+ /* Don't start if the VirtualBox settings directory does not exist. */
+ char szUserHomeDir[RTPATH_MAX];
+ rc = com::GetVBoxUserHomeDirectory(szUserHomeDir, sizeof(szUserHomeDir), false /* fCreateDir */);
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory: %Rrc", rc);
+ else if (!RTDirExists(szUserHomeDir))
+ return RTEXITCODE_SUCCESS;
+
+ /* create release logger, to stdout */
+ RTERRINFOSTATIC ErrInfo;
+ rc = com::VBoxLogRelCreate("Autostart", g_fDaemonize ? NULL : pszLogFile,
+ RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
+ "all", "VBOXAUTOSTART_RELEASE_LOG",
+ RTLOGDEST_STDOUT, UINT32_MAX /* cMaxEntriesPerGroup */,
+ g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
+ RTErrInfoInitStatic(&ErrInfo));
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", ErrInfo.Core.pszMsg, rc);
+
+#ifdef VBOXAUTOSTART_DAEMONIZE
+ if (g_fDaemonize)
+ {
+ /* prepare release logging */
+ char szLogFile[RTPATH_MAX];
+
+ if (!pszLogFile || !*pszLogFile)
+ {
+ rc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory for logging: %Rrc", rc);
+ rc = RTPathAppend(szLogFile, sizeof(szLogFile), "vboxautostart.log");
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not construct logging path: %Rrc", rc);
+ pszLogFile = szLogFile;
+ }
+
+ rc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, NULL);
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
+ /* create release logger, to file */
+ rc = com::VBoxLogRelCreate("Autostart", pszLogFile,
+ RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
+ "all", "VBOXAUTOSTART_RELEASE_LOG",
+ RTLOGDEST_FILE, UINT32_MAX /* cMaxEntriesPerGroup */,
+ g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
+ RTErrInfoInitStatic(&ErrInfo));
+ if (RT_FAILURE(rc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", ErrInfo.Core.pszMsg, rc);
+ }
+#endif
+
+ /* Set up COM */
+ rc = autostartSetup();
+ if (RT_FAILURE(rc))
+ return RTEXITCODE_FAILURE;
+
+ if (fStart)
+ rc = autostartStartMain(pCfgAstUser);
+ else
+ {
+ Assert(fStop);
+ rc = autostartStopMain(pCfgAstUser);
+ }
+
+ autostartConfigAstDestroy(pCfgAst);
+ NativeEventQueue::getMainEventQueue()->processEventQueue(0);
+ autostartShutdown();
+
+ return RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
+}
+
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp
new file mode 100644
index 00000000..dd25c1b9
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp
@@ -0,0 +1,1493 @@
+/* $Id: VBoxAutostart-win.cpp $ */
+/** @file
+ * VirtualBox Autostart Service - Windows Specific Code.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+
+/*********************************************************************************************************************************
+* Header Files *
+*********************************************************************************************************************************/
+#include <iprt/dir.h>
+#include <iprt/env.h>
+#include <iprt/err.h>
+#include <iprt/getopt.h>
+#include <iprt/initterm.h>
+#include <iprt/mem.h>
+#include <iprt/message.h>
+#include <iprt/process.h>
+#include <iprt/path.h>
+#include <iprt/semaphore.h>
+#include <iprt/stream.h>
+#include <iprt/string.h>
+#include <iprt/thread.h>
+
+#include <iprt/win/windows.h>
+#include <ntsecapi.h>
+
+#define SECURITY_WIN32
+#include <Security.h>
+
+#include <VBox/com/array.h>
+#include <VBox/com/com.h>
+#include <VBox/com/ErrorInfo.h>
+#include <VBox/com/errorprint.h>
+#include <VBox/com/Guid.h>
+#include <VBox/com/listeners.h>
+#include <VBox/com/NativeEventQueue.h>
+#include <VBox/com/string.h>
+#include <VBox/com/VirtualBox.h>
+
+#include <VBox/log.h>
+
+#include "VBoxAutostart.h"
+#include "PasswordInput.h"
+
+
+/*********************************************************************************************************************************
+* Defined Constants And Macros *
+*********************************************************************************************************************************/
+/** The service name. */
+#define AUTOSTART_SERVICE_NAME "VBoxAutostartSvc"
+/** The service display name. */
+#define AUTOSTART_SERVICE_DISPLAY_NAME "VirtualBox Autostart Service"
+
+/* just define it here instead of including
+ * a bunch of nt headers */
+#ifndef STATUS_SUCCESS
+#define STATUS_SUCCESS ((NTSTATUS)0)
+#endif
+
+
+ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL;
+bool g_fVerbose = false;
+ComPtr<IVirtualBox> g_pVirtualBox = NULL;
+ComPtr<ISession> g_pSession = NULL;
+
+
+/*********************************************************************************************************************************
+* Global Variables *
+*********************************************************************************************************************************/
+/** The service control handler handle. */
+static SERVICE_STATUS_HANDLE g_hSupSvcWinCtrlHandler = NULL;
+/** The service status. */
+static uint32_t volatile g_u32SupSvcWinStatus = SERVICE_STOPPED;
+/** The semaphore the main service thread is waiting on in autostartSvcWinServiceMain. */
+static RTSEMEVENTMULTI g_hSupSvcWinEvent = NIL_RTSEMEVENTMULTI;
+/** The service name is used for send to service main. */
+static com::Bstr g_bstrServiceName;
+
+/** Verbosity level. */
+unsigned g_cVerbosity = 0;
+
+/** Logging parameters. */
+static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
+static uint32_t g_uHistoryFileTime = 0; /* No time limit, it's very low volume. */
+static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
+
+
+/*********************************************************************************************************************************
+* Internal Functions *
+*********************************************************************************************************************************/
+static SC_HANDLE autostartSvcWinOpenSCManager(const char *pszAction, DWORD dwAccess);
+
+static int autostartGetProcessDomainUser(com::Utf8Str &aUser)
+{
+ int rc = VERR_NOT_SUPPORTED;
+
+ RTUTF16 wszUsername[1024] = { 0 };
+ ULONG cwcUsername = RT_ELEMENTS(wszUsername);
+ char *pszUser = NULL;
+ if (!GetUserNameExW(NameSamCompatible, &wszUsername[0], &cwcUsername))
+ return RTErrConvertFromWin32(GetLastError());
+ rc = RTUtf16ToUtf8(wszUsername, &pszUser);
+ aUser = pszUser;
+ aUser.toLower();
+ RTStrFree(pszUser);
+ return rc;
+}
+
+static int autostartGetLocalDomain(com::Utf8Str &aDomain)
+{
+ RTUTF16 pwszDomain[MAX_COMPUTERNAME_LENGTH + 1] = { 0 };
+ uint32_t cwcDomainSize = MAX_COMPUTERNAME_LENGTH + 1;
+ if (!GetComputerNameW(pwszDomain, (LPDWORD)&cwcDomainSize))
+ return RTErrConvertFromWin32(GetLastError());
+ char *pszDomain = NULL;
+ int rc = RTUtf16ToUtf8(pwszDomain, &pszDomain);
+ aDomain = pszDomain;
+ aDomain.toLower();
+ RTStrFree(pszDomain);
+ return rc;
+}
+
+static int autostartGetDomainAndUser(const com::Utf8Str &aDomainAndUser, com::Utf8Str &aDomain, com::Utf8Str &aUser)
+{
+ size_t offDelim = aDomainAndUser.find("\\");
+ if (offDelim != aDomainAndUser.npos)
+ {
+ // if only domain is specified
+ if (aDomainAndUser.length() - offDelim == 1)
+ return VERR_INVALID_PARAMETER;
+
+ if (offDelim == 1 && aDomainAndUser[0] == '.')
+ {
+ int rc = autostartGetLocalDomain(aDomain);
+ aUser = aDomainAndUser.substr(offDelim + 1);
+ return rc;
+ }
+ aDomain = aDomainAndUser.substr(0, offDelim);
+ aUser = aDomainAndUser.substr(offDelim + 1);
+ aDomain.toLower();
+ aUser.toLower();
+ return VINF_SUCCESS;
+ }
+
+ offDelim = aDomainAndUser.find("@");
+ if (offDelim != aDomainAndUser.npos)
+ {
+ // if only domain is specified
+ if (offDelim == 0)
+ return VERR_INVALID_PARAMETER;
+
+ // with '@' but without domain
+ if (aDomainAndUser.length() - offDelim == 1)
+ {
+ int rc = autostartGetLocalDomain(aDomain);
+ aUser = aDomainAndUser.substr(0, offDelim);
+ return rc;
+ }
+ aDomain = aDomainAndUser.substr(offDelim + 1);
+ aUser = aDomainAndUser.substr(0, offDelim);
+ aDomain.toLower();
+ aUser.toLower();
+ return VINF_SUCCESS;
+ }
+
+ // only user is specified
+ int rc = autostartGetLocalDomain(aDomain);
+ aUser = aDomainAndUser;
+ aDomain.toLower();
+ aUser.toLower();
+ return rc;
+}
+
+/** Common helper for formatting the service name. */
+static void autostartFormatServiceName(const com::Utf8Str &aDomain, const com::Utf8Str &aUser, com::Utf8Str &aServiceName)
+{
+ aServiceName.printf("%s%s%s", AUTOSTART_SERVICE_NAME, aDomain.c_str(), aUser.c_str());
+}
+
+/** Used by the delete service operation. */
+static int autostartGetServiceName(const com::Utf8Str &aDomainAndUser, com::Utf8Str &aServiceName)
+{
+ com::Utf8Str sDomain;
+ com::Utf8Str sUser;
+ int rc = autostartGetDomainAndUser(aDomainAndUser, sDomain, sUser);
+ if (RT_FAILURE(rc))
+ return rc;
+ autostartFormatServiceName(sDomain, sUser, aServiceName);
+ return VINF_SUCCESS;
+}
+
+/**
+ * Print out progress on the console.
+ *
+ * This runs the main event queue every now and then to prevent piling up
+ * unhandled things (which doesn't cause real problems, just makes things
+ * react a little slower than in the ideal case).
+ */
+DECLHIDDEN(HRESULT) showProgress(ComPtr<IProgress> progress)
+{
+ using namespace com;
+
+ BOOL fCompleted = FALSE;
+ ULONG uCurrentPercent = 0;
+ Bstr bstrOperationDescription;
+
+ NativeEventQueue::getMainEventQueue()->processEventQueue(0);
+
+ ULONG cOperations = 1;
+ HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
+ if (FAILED(hrc))
+ return hrc;
+
+ /* setup signal handling if cancelable */
+ bool fCanceledAlready = false;
+ BOOL fCancelable;
+ hrc = progress->COMGETTER(Cancelable)(&fCancelable);
+ if (FAILED(hrc))
+ fCancelable = FALSE;
+
+ hrc = progress->COMGETTER(Completed(&fCompleted));
+ while (SUCCEEDED(hrc))
+ {
+ progress->COMGETTER(Percent(&uCurrentPercent));
+
+ if (fCompleted)
+ break;
+
+ /* process async cancelation */
+ if (!fCanceledAlready)
+ {
+ hrc = progress->Cancel();
+ if (SUCCEEDED(hrc))
+ fCanceledAlready = true;
+ }
+
+ /* make sure the loop is not too tight */
+ progress->WaitForCompletion(100);
+
+ NativeEventQueue::getMainEventQueue()->processEventQueue(0);
+ hrc = progress->COMGETTER(Completed(&fCompleted));
+ }
+
+ /* complete the line. */
+ LONG iRc = E_FAIL;
+ hrc = progress->COMGETTER(ResultCode)(&iRc);
+ if (SUCCEEDED(hrc))
+ {
+ hrc = iRc;
+ }
+
+ return hrc;
+}
+
+DECLHIDDEN(void) autostartSvcOsLogStr(const char *pszMsg, AUTOSTARTLOGTYPE enmLogType)
+{
+ /* write it to the console + release log too (if configured). */
+ LogRel(("%s", pszMsg));
+
+ /** @todo r=andy Only (un)register source once? */
+ HANDLE hEventLog = RegisterEventSourceA(NULL /* local computer */, "VBoxAutostartSvc");
+ AssertReturnVoid(hEventLog != NULL);
+ WORD wType = 0;
+ const char *apsz[2];
+ apsz[0] = "VBoxAutostartSvc";
+ apsz[1] = pszMsg;
+
+ switch (enmLogType)
+ {
+ case AUTOSTARTLOGTYPE_INFO:
+ RTStrmPrintf(g_pStdOut, "%s", pszMsg);
+ wType = 0;
+ break;
+ case AUTOSTARTLOGTYPE_ERROR:
+ RTStrmPrintf(g_pStdErr, "Error: %s", pszMsg);
+ wType = EVENTLOG_ERROR_TYPE;
+ break;
+ case AUTOSTARTLOGTYPE_WARNING:
+ RTStrmPrintf(g_pStdOut, "Warning: %s", pszMsg);
+ wType = EVENTLOG_WARNING_TYPE;
+ break;
+ case AUTOSTARTLOGTYPE_VERBOSE:
+ RTStrmPrintf(g_pStdOut, "%s", pszMsg);
+ wType = EVENTLOG_INFORMATION_TYPE;
+ break;
+ default:
+ AssertMsgFailed(("Invalid log type %#x\n", enmLogType));
+ break;
+ }
+
+ /** @todo r=andy Why ANSI and not Unicode (xxxW)? */
+ BOOL fRc = ReportEventA(hEventLog, /* hEventLog */
+ wType, /* wType */
+ 0, /* wCategory */
+ 0 /** @todo mc */, /* dwEventID */
+ NULL, /* lpUserSid */
+ RT_ELEMENTS(apsz), /* wNumStrings */
+ 0, /* dwDataSize */
+ apsz, /* lpStrings */
+ NULL); /* lpRawData */
+ AssertMsg(fRc, ("ReportEventA failed with %ld\n", GetLastError())); RT_NOREF(fRc);
+ DeregisterEventSource(hEventLog);
+}
+
+
+/**
+ * Adds "logon as service" policy to user rights
+ *
+ * When this fails, an error message will be displayed.
+ *
+ * @returns VBox status code.
+ *
+ * @param sUser The name of user whom the policy should be added.
+ */
+static int autostartUpdatePolicy(const com::Utf8Str &sUser)
+{
+ LSA_OBJECT_ATTRIBUTES objectAttributes;
+ /* Object attributes are reserved, so initialize to zeros. */
+ RT_ZERO(objectAttributes);
+
+ int vrc;
+
+ /* Get a handle to the Policy object. */
+ LSA_HANDLE hPolicy;
+ NTSTATUS ntRc = LsaOpenPolicy( NULL, &objectAttributes, POLICY_ALL_ACCESS, &hPolicy);
+ if (ntRc != STATUS_SUCCESS)
+ {
+ DWORD dwErr = LsaNtStatusToWinError(ntRc);
+ vrc = RTErrConvertFromWin32(dwErr);
+ autostartSvcDisplayError("LsaOpenPolicy failed rc=%Rrc (%#x)\n", vrc, dwErr);
+ return vrc;
+ }
+ /* Get user SID */
+ DWORD cbDomain = 0;
+ SID_NAME_USE enmSidUse = SidTypeUser;
+ RTUTF16 *pwszUser = NULL;
+ size_t cwUser = 0;
+ vrc = RTStrToUtf16Ex(sUser.c_str(), sUser.length(), &pwszUser, 0, &cwUser);
+ if (RT_SUCCESS(vrc))
+ {
+ PSID pSid = NULL;
+ DWORD cbSid = 0;
+ if (!LookupAccountNameW( NULL, pwszUser, pSid, &cbSid, NULL, &cbDomain, &enmSidUse))
+ {
+ DWORD dwErr = GetLastError();
+ if (dwErr == ERROR_INSUFFICIENT_BUFFER)
+ {
+ pSid = (PSID)RTMemAllocZ(cbSid);
+ if (pSid != NULL)
+ {
+ PRTUTF16 pwszDomain = (PRTUTF16)RTMemAllocZ(cbDomain * sizeof(RTUTF16));
+ if (pwszDomain != NULL)
+ {
+ if (LookupAccountNameW( NULL, pwszUser, pSid, &cbSid, pwszDomain, &cbDomain, &enmSidUse))
+ {
+ if (enmSidUse != SidTypeUser)
+ {
+ vrc = VERR_INVALID_PARAMETER;
+ autostartSvcDisplayError("The name %s is not the user\n", sUser.c_str());
+ }
+ else
+ {
+ /* Add privilege */
+ LSA_UNICODE_STRING lwsPrivilege;
+ // Create an LSA_UNICODE_STRING for the privilege names.
+ lwsPrivilege.Buffer = L"SeServiceLogonRight";
+ size_t cwPrivilege = wcslen(lwsPrivilege.Buffer);
+ lwsPrivilege.Length = (USHORT)cwPrivilege * sizeof(WCHAR);
+ lwsPrivilege.MaximumLength = (USHORT)(cwPrivilege + 1) * sizeof(WCHAR);
+ ntRc = LsaAddAccountRights(hPolicy, pSid, &lwsPrivilege, 1);
+ if (ntRc != STATUS_SUCCESS)
+ {
+ dwErr = LsaNtStatusToWinError(ntRc);
+ vrc = RTErrConvertFromWin32(dwErr);
+ autostartSvcDisplayError("LsaAddAccountRights failed rc=%Rrc (%#x)\n", vrc, dwErr);
+ }
+ }
+ }
+ else
+ {
+ dwErr = GetLastError();
+ vrc = RTErrConvertFromWin32(dwErr);
+ autostartSvcDisplayError("LookupAccountName failed rc=%Rrc (%#x)\n", vrc, dwErr);
+ }
+ RTMemFree(pwszDomain);
+ }
+ else
+ {
+ vrc = VERR_NO_MEMORY;
+ autostartSvcDisplayError("autostartUpdatePolicy failed rc=%Rrc\n", vrc);
+ }
+
+ RTMemFree(pSid);
+ }
+ else
+ {
+ vrc = VERR_NO_MEMORY;
+ autostartSvcDisplayError("autostartUpdatePolicy failed rc=%Rrc\n", vrc);
+ }
+ }
+ else
+ {
+ vrc = RTErrConvertFromWin32(dwErr);
+ autostartSvcDisplayError("LookupAccountName failed rc=%Rrc (%#x)\n", vrc, dwErr);
+ }
+ }
+ }
+ else
+ autostartSvcDisplayError("Failed to convert user name rc=%Rrc\n", vrc);
+
+ if (pwszUser != NULL)
+ RTUtf16Free(pwszUser);
+
+ LsaClose(hPolicy);
+ return vrc;
+}
+
+
+/**
+ * Opens the service control manager.
+ *
+ * When this fails, an error message will be displayed.
+ *
+ * @returns Valid handle on success.
+ * NULL on failure, will display an error message.
+ *
+ * @param pszAction The action which is requesting access to SCM.
+ * @param dwAccess The desired access.
+ */
+static SC_HANDLE autostartSvcWinOpenSCManager(const char *pszAction, DWORD dwAccess)
+{
+ SC_HANDLE hSCM = OpenSCManager(NULL /* lpMachineName*/, NULL /* lpDatabaseName */, dwAccess);
+ if (hSCM == NULL)
+ {
+ DWORD err = GetLastError();
+ switch (err)
+ {
+ case ERROR_ACCESS_DENIED:
+ autostartSvcDisplayError("%s - OpenSCManager failure: access denied\n", pszAction);
+ break;
+ default:
+ autostartSvcDisplayError("%s - OpenSCManager failure: %d\n", pszAction, err);
+ break;
+ }
+ }
+ return hSCM;
+}
+
+
+/**
+ * Opens the service.
+ *
+ * Last error is preserved on failure and set to 0 on success.
+ *
+ * @returns Valid service handle on success.
+ * NULL on failure, will display an error message unless it's ignored.
+ * Use GetLastError() to find out what the last Windows error was.
+ *
+ * @param pszAction The action which is requesting access to the service.
+ * @param dwSCMAccess The service control manager access.
+ * @param dwSVCAccess The desired service access.
+ * @param cIgnoredErrors The number of ignored errors.
+ * @param ... Errors codes that should not cause a message to be displayed.
+ */
+static SC_HANDLE autostartSvcWinOpenService(const PRTUTF16 pwszServiceName, const char *pszAction, DWORD dwSCMAccess, DWORD dwSVCAccess,
+ unsigned cIgnoredErrors, ...)
+{
+ SC_HANDLE hSCM = autostartSvcWinOpenSCManager(pszAction, dwSCMAccess);
+ if (!hSCM)
+ return NULL;
+
+ SC_HANDLE hSvc = OpenServiceW(hSCM, pwszServiceName, dwSVCAccess);
+ if (hSvc)
+ {
+ CloseServiceHandle(hSCM);
+ SetLastError(0);
+ }
+ else
+ {
+ DWORD const dwErr = GetLastError();
+ bool fIgnored = false;
+ va_list va;
+ va_start(va, cIgnoredErrors);
+ while (!fIgnored && cIgnoredErrors-- > 0)
+ fIgnored = (DWORD)va_arg(va, int) == dwErr;
+ va_end(va);
+ if (!fIgnored)
+ {
+ switch (dwErr)
+ {
+ case ERROR_ACCESS_DENIED:
+ autostartSvcDisplayError("%s - OpenService failure: access denied\n", pszAction);
+ break;
+ case ERROR_SERVICE_DOES_NOT_EXIST:
+ autostartSvcDisplayError("%s - OpenService failure: The service %ls does not exist. Reinstall it.\n",
+ pszAction, pwszServiceName);
+ break;
+ default:
+ autostartSvcDisplayError("%s - OpenService failure, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
+ break;
+ }
+ }
+
+ CloseServiceHandle(hSCM);
+ SetLastError(dwErr);
+ }
+ return hSvc;
+}
+
+static RTEXITCODE autostartSvcWinInterrogate(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"interrogate\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinStop(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"stop\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinContinue(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"continue\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinPause(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"pause\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinStart(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"start\" action is not implemented.\n");
+ return RTEXITCODE_SUCCESS;
+}
+
+
+static RTEXITCODE autostartSvcWinQueryDescription(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"qdescription\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinQueryConfig(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"qconfig\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+static RTEXITCODE autostartSvcWinDisable(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"disable\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+static RTEXITCODE autostartSvcWinEnable(int argc, char **argv)
+{
+ RT_NOREF(argc, argv);
+ RTPrintf("VBoxAutostartSvc: The \"enable\" action is not implemented.\n");
+ return RTEXITCODE_FAILURE;
+}
+
+
+/**
+ * Handle the 'delete' action.
+ *
+ * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
+ * @param argc The action argument count.
+ * @param argv The action argument vector.
+ */
+static RTEXITCODE autostartSvcWinDelete(int argc, char **argv)
+{
+ /*
+ * Parse the arguments.
+ */
+ const char *pszUser = NULL;
+ static const RTGETOPTDEF s_aOptions[] =
+ {
+ { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
+ { "--user", 'u', RTGETOPT_REQ_STRING },
+ };
+ int ch;
+ RTGETOPTUNION Value;
+ RTGETOPTSTATE GetState;
+ RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
+ while ((ch = RTGetOpt(&GetState, &Value)))
+ {
+ switch (ch)
+ {
+ case 'v':
+ g_cVerbosity++;
+ break;
+ case 'u':
+ pszUser = Value.psz;
+ break;
+ default:
+ return autostartSvcDisplayGetOptError("delete", ch, &Value);
+ }
+ }
+
+ if (!pszUser)
+ return autostartSvcDisplayError("delete - DeleteService failed, user name required.\n");
+
+ com::Utf8Str sServiceName;
+ int vrc = autostartGetServiceName(pszUser, sServiceName);
+ if (RT_FAILURE(vrc))
+ return autostartSvcDisplayError("delete - DeleteService failed, service name for user %s cannot be constructed.\n",
+ pszUser);
+ /*
+ * Delete the service.
+ */
+ RTEXITCODE rcExit = RTEXITCODE_FAILURE;
+ SC_HANDLE hSvc = autostartSvcWinOpenService(com::Bstr(sServiceName).raw(), "delete", SERVICE_CHANGE_CONFIG, DELETE, 0);
+ if (hSvc)
+ {
+ if (DeleteService(hSvc))
+ {
+ if (g_cVerbosity)
+ RTPrintf("Successfully deleted the %s service.\n", sServiceName.c_str());
+ rcExit = RTEXITCODE_SUCCESS;
+ }
+ else
+ {
+ DWORD const dwErr = GetLastError();
+ autostartSvcDisplayError("delete - DeleteService failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
+ }
+ CloseServiceHandle(hSvc);
+ }
+ return rcExit;
+}
+
+
+/**
+ * Handle the 'create' action.
+ *
+ * @returns 0 or 1.
+ * @param argc The action argument count.
+ * @param argv The action argument vector.
+ */
+static RTEXITCODE autostartSvcWinCreate(int argc, char **argv)
+{
+ /*
+ * Parse the arguments.
+ */
+ const char *pszUser = NULL;
+ com::Utf8Str strPwd;
+ const char *pszPwdFile = NULL;
+ static const RTGETOPTDEF s_aOptions[] =
+ {
+ /* Common options first. */
+ { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
+ { "--user", 'u', RTGETOPT_REQ_STRING },
+ { "--username", 'u', RTGETOPT_REQ_STRING },
+ { "--password-file", 'p', RTGETOPT_REQ_STRING }
+ };
+ int ch;
+ RTGETOPTUNION Value;
+ RTGETOPTSTATE GetState;
+ RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
+ while ((ch = RTGetOpt(&GetState, &Value)))
+ {
+ switch (ch)
+ {
+ /* Common options first. */
+ case 'v':
+ g_cVerbosity++;
+ break;
+ case 'u':
+ pszUser = Value.psz;
+ break;
+ case 'p':
+ pszPwdFile = Value.psz;
+ break;
+ default:
+ return autostartSvcDisplayGetOptError("create", ch, &Value);
+ }
+ }
+
+ if (!pszUser)
+ return autostartSvcDisplayError("Username is missing");
+
+ if (pszPwdFile)
+ {
+ /* Get password from file. */
+ RTEXITCODE rcExit = readPasswordFile(pszPwdFile, &strPwd);
+ if (rcExit == RTEXITCODE_FAILURE)
+ return rcExit;
+ }
+ else
+ {
+ /* Get password from console. */
+ RTEXITCODE rcExit = readPasswordFromConsole(&strPwd, "Enter password:");
+ if (rcExit == RTEXITCODE_FAILURE)
+ return rcExit;
+ }
+
+ if (strPwd.isEmpty())
+ return autostartSvcDisplayError("Password is missing");
+
+ com::Utf8Str sDomain;
+ com::Utf8Str sUserTmp;
+ int vrc = autostartGetDomainAndUser(pszUser, sDomain, sUserTmp);
+ if (RT_FAILURE(vrc))
+ return autostartSvcDisplayError("create - Failed to get domain and user from string '%s' (%Rrc)\n",
+ pszUser, vrc);
+ com::Utf8StrFmt sUserFullName("%s\\%s", sDomain.c_str(), sUserTmp.c_str());
+ com::Utf8StrFmt sDisplayName("%s %s@%s", AUTOSTART_SERVICE_DISPLAY_NAME, sUserTmp.c_str(), sDomain.c_str());
+ com::Utf8Str sServiceName;
+ autostartFormatServiceName(sDomain, sUserTmp, sServiceName);
+
+ vrc = autostartUpdatePolicy(sUserFullName);
+ if (RT_FAILURE(vrc))
+ return autostartSvcDisplayError("Failed to get/update \"logon as service\" policy for user %s (%Rrc)\n",
+ sUserFullName.c_str(), vrc);
+ /*
+ * Create the service.
+ */
+ RTEXITCODE rcExit = RTEXITCODE_FAILURE;
+ SC_HANDLE hSCM = autostartSvcWinOpenSCManager("create", SC_MANAGER_CREATE_SERVICE); /*SC_MANAGER_ALL_ACCESS*/
+ if (hSCM)
+ {
+ char szExecPath[RTPATH_MAX];
+ if (RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
+ {
+ if (g_cVerbosity)
+ RTPrintf("Creating the %s service, binary \"%s\"...\n",
+ sServiceName.c_str(), szExecPath); /* yea, the binary name isn't UTF-8, but wtf. */
+
+ /*
+ * Add service name as command line parameter for the service
+ */
+ com::Utf8StrFmt sCmdLine("\"%s\" --service=%s", szExecPath, sServiceName.c_str());
+ com::Bstr bstrServiceName(sServiceName);
+ com::Bstr bstrDisplayName(sDisplayName);
+ com::Bstr bstrCmdLine(sCmdLine);
+ com::Bstr bstrUserFullName(sUserFullName);
+ com::Bstr bstrPwd(strPwd);
+
+ SC_HANDLE hSvc = CreateServiceW(hSCM, /* hSCManager */
+ bstrServiceName.raw(), /* lpServiceName */
+ bstrDisplayName.raw(), /* lpDisplayName */
+ SERVICE_CHANGE_CONFIG | SERVICE_QUERY_STATUS | SERVICE_QUERY_CONFIG, /* dwDesiredAccess */
+ SERVICE_WIN32_OWN_PROCESS, /* dwServiceType ( | SERVICE_INTERACTIVE_PROCESS? ) */
+ SERVICE_AUTO_START, /* dwStartType */
+ SERVICE_ERROR_NORMAL, /* dwErrorControl */
+ bstrCmdLine.raw(), /* lpBinaryPathName */
+ NULL, /* lpLoadOrderGroup */
+ NULL, /* lpdwTagId */
+ L"Winmgmt\0RpcSs\0\0", /* lpDependencies */
+ bstrUserFullName.raw(), /* lpServiceStartName (NULL => LocalSystem) */
+ bstrPwd.raw()); /* lpPassword */
+ if (hSvc)
+ {
+ RTPrintf("Successfully created the %s service.\n", sServiceName.c_str());
+ /** @todo Set the service description or it'll look weird in the vista service manager.
+ * Anything else that should be configured? Start access or something? */
+ rcExit = RTEXITCODE_SUCCESS;
+ CloseServiceHandle(hSvc);
+ }
+ else
+ {
+ DWORD const dwErr = GetLastError();
+ switch (dwErr)
+ {
+ case ERROR_SERVICE_EXISTS:
+ autostartSvcDisplayError("create - The service already exists!\n");
+ break;
+ default:
+ autostartSvcDisplayError("create - CreateService failed, rc=%Rrc (%#x)\n",
+ RTErrConvertFromWin32(dwErr), dwErr);
+ break;
+ }
+ }
+ CloseServiceHandle(hSvc);
+ }
+ else
+ autostartSvcDisplayError("create - Failed to obtain the executable path\n");
+ }
+ return rcExit;
+}
+
+
+/**
+ * Sets the service status, just a SetServiceStatus Wrapper.
+ *
+ * @returns See SetServiceStatus.
+ * @param dwStatus The current status.
+ * @param iWaitHint The wait hint, if < 0 then supply a default.
+ * @param dwExitCode The service exit code.
+ */
+static bool autostartSvcWinSetServiceStatus(DWORD dwStatus, int iWaitHint, DWORD dwExitCode)
+{
+ SERVICE_STATUS SvcStatus;
+ SvcStatus.dwServiceType = SERVICE_WIN32_OWN_PROCESS;
+ SvcStatus.dwWin32ExitCode = dwExitCode;
+ SvcStatus.dwServiceSpecificExitCode = 0;
+ SvcStatus.dwWaitHint = iWaitHint >= 0 ? iWaitHint : 3000;
+ SvcStatus.dwCurrentState = dwStatus;
+ LogFlow(("autostartSvcWinSetServiceStatus: %d -> %d\n", g_u32SupSvcWinStatus, dwStatus));
+ g_u32SupSvcWinStatus = dwStatus;
+ switch (dwStatus)
+ {
+ case SERVICE_START_PENDING:
+ SvcStatus.dwControlsAccepted = 0;
+ break;
+ default:
+ SvcStatus.dwControlsAccepted
+ = SERVICE_ACCEPT_STOP
+ | SERVICE_ACCEPT_SHUTDOWN;
+ break;
+ }
+
+ static DWORD dwCheckPoint = 0;
+ switch (dwStatus)
+ {
+ case SERVICE_RUNNING:
+ case SERVICE_STOPPED:
+ SvcStatus.dwCheckPoint = 0;
+ default:
+ SvcStatus.dwCheckPoint = ++dwCheckPoint;
+ break;
+ }
+ return SetServiceStatus(g_hSupSvcWinCtrlHandler, &SvcStatus) != FALSE;
+}
+
+
+/**
+ * Service control handler (extended).
+ *
+ * @returns Windows status (see HandlerEx).
+ * @retval NO_ERROR if handled.
+ * @retval ERROR_CALL_NOT_IMPLEMENTED if not handled.
+ *
+ * @param dwControl The control code.
+ * @param dwEventType Event type. (specific to the control?)
+ * @param pvEventData Event data, specific to the event.
+ * @param pvContext The context pointer registered with the handler.
+ * Currently not used.
+ */
+static DWORD WINAPI
+autostartSvcWinServiceCtrlHandlerEx(DWORD dwControl, DWORD dwEventType, LPVOID pvEventData, LPVOID pvContext) RT_NOTHROW_DEF
+{
+ RT_NOREF(dwEventType);
+ RT_NOREF(pvEventData);
+ RT_NOREF(pvContext);
+
+ LogFlow(("autostartSvcWinServiceCtrlHandlerEx: dwControl=%#x dwEventType=%#x pvEventData=%p\n",
+ dwControl, dwEventType, pvEventData));
+
+ switch (dwControl)
+ {
+ /*
+ * Interrogate the service about it's current status.
+ * MSDN says that this should just return NO_ERROR and does
+ * not need to set the status again.
+ */
+ case SERVICE_CONTROL_INTERROGATE:
+ return NO_ERROR;
+
+ /*
+ * Request to stop the service.
+ */
+ case SERVICE_CONTROL_SHUTDOWN:
+ case SERVICE_CONTROL_STOP:
+ {
+ if (dwControl == SERVICE_CONTROL_SHUTDOWN)
+ autostartSvcLogVerbose(1, "SERVICE_CONTROL_SHUTDOWN\n");
+ else
+ autostartSvcLogVerbose(1, "SERVICE_CONTROL_STOP\n");
+
+ /*
+ * Check if the real services can be stopped and then tell them to stop.
+ */
+ autostartSvcWinSetServiceStatus(SERVICE_STOP_PENDING, 3000, NO_ERROR);
+
+ /*
+ * Notify the main thread that we're done, it will wait for the
+ * VMs to stop, and set the windows service status to SERVICE_STOPPED
+ * and return.
+ */
+ int rc = RTSemEventMultiSignal(g_hSupSvcWinEvent);
+ if (RT_FAILURE(rc)) /** @todo r=andy Don't we want to report back an error here to SCM? */
+ autostartSvcLogErrorRc(rc, "SERVICE_CONTROL_STOP: RTSemEventMultiSignal failed, %Rrc\n", rc);
+
+ return NO_ERROR;
+ }
+
+ default:
+ /*
+ * We only expect to receive controls we explicitly listed
+ * in SERVICE_STATUS::dwControlsAccepted. Logged in hex
+ * b/c WinSvc.h defines them in hex
+ */
+ autostartSvcLogWarning("Unexpected service control message 0x%RX64\n", (uint64_t)dwControl);
+ break;
+ }
+
+ return ERROR_CALL_NOT_IMPLEMENTED;
+}
+
+static int autostartStartVMs(void)
+{
+ int rc = autostartSetup();
+ if (RT_FAILURE(rc))
+ return rc;
+
+ const char *pszConfigFile = RTEnvGet("VBOXAUTOSTART_CONFIG");
+ if (!pszConfigFile)
+ return autostartSvcLogErrorRc(VERR_ENV_VAR_NOT_FOUND,
+ "Starting VMs failed. VBOXAUTOSTART_CONFIG environment variable is not defined.\n");
+ bool fAllow = false;
+
+ PCFGAST pCfgAst = NULL;
+ rc = autostartParseConfig(pszConfigFile, &pCfgAst);
+ if (RT_FAILURE(rc))
+ return autostartSvcLogErrorRc(rc, "Starting VMs failed. Failed to parse the config file. Check the access permissions and file structure.\n");
+ PCFGAST pCfgAstPolicy = autostartConfigAstGetByName(pCfgAst, "default_policy");
+ /* Check default policy. */
+ if (pCfgAstPolicy)
+ {
+ if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
+ && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow")
+ || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "deny")))
+ {
+ if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow"))
+ fAllow = true;
+ }
+ else
+ {
+ autostartConfigAstDestroy(pCfgAst);
+ return autostartSvcLogErrorRc(VERR_INVALID_PARAMETER, "'default_policy' must be either 'allow' or 'deny'.\n");
+ }
+ }
+
+ com::Utf8Str sUser;
+ rc = autostartGetProcessDomainUser(sUser);
+ if (RT_FAILURE(rc))
+ {
+ autostartConfigAstDestroy(pCfgAst);
+ return autostartSvcLogErrorRc(rc, "Failed to query username of the process (%Rrc).\n", rc);
+ }
+
+ PCFGAST pCfgAstUser = NULL;
+ for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
+ {
+ PCFGAST pNode = pCfgAst->u.Compound.apAstNodes[i];
+ com::Utf8Str sDomain;
+ com::Utf8Str sUserTmp;
+ rc = autostartGetDomainAndUser(pNode->pszKey, sDomain, sUserTmp);
+ if (RT_FAILURE(rc))
+ continue;
+ com::Utf8StrFmt sDomainUser("%s\\%s", sDomain.c_str(), sUserTmp.c_str());
+ if (sDomainUser == sUser)
+ {
+ pCfgAstUser = pNode;
+ break;
+ }
+ }
+
+ if ( pCfgAstUser
+ && pCfgAstUser->enmType == CFGASTNODETYPE_COMPOUND)
+ {
+ pCfgAstPolicy = autostartConfigAstGetByName(pCfgAstUser, "allow");
+ if (pCfgAstPolicy)
+ {
+ if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
+ && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true")
+ || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "false")))
+ fAllow = RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true") == 0;
+ else
+ {
+ autostartConfigAstDestroy(pCfgAst);
+ return autostartSvcLogErrorRc(VERR_INVALID_PARAMETER, "'allow' must be either 'true' or 'false'.\n");
+ }
+ }
+ }
+ else if (pCfgAstUser)
+ {
+ autostartConfigAstDestroy(pCfgAst);
+ return autostartSvcLogErrorRc(VERR_INVALID_PARAMETER, "Invalid config, user is not a compound node.\n");
+ }
+
+ if (!fAllow)
+ {
+ autostartConfigAstDestroy(pCfgAst);
+ return autostartSvcLogErrorRc(VERR_INVALID_PARAMETER, "User is not allowed to autostart VMs.\n");
+ }
+
+ if (RT_SUCCESS(rc))
+ rc = autostartStartMain(pCfgAstUser);
+
+ autostartConfigAstDestroy(pCfgAst);
+
+ return rc;
+}
+
+/**
+ * Windows Service Main.
+ *
+ * This is invoked when the service is started and should not return until
+ * the service has been stopped.
+ *
+ * @param cArgs Argument count.
+ * @param papwszArgs Argument vector.
+ */
+static VOID WINAPI autostartSvcWinServiceMain(DWORD cArgs, LPWSTR *papwszArgs)
+{
+ RT_NOREF(cArgs, papwszArgs);
+ LogFlowFuncEnter();
+
+ /* Give this thread a name in the logs. */
+ RTThreadAdopt(RTTHREADTYPE_DEFAULT, 0, "service", NULL);
+
+#if 0
+ for (size_t i = 0; i < cArgs; ++i)
+ LogRel(("arg[%zu] = %ls\n", i, papwszArgs[i]));
+#endif
+
+ DWORD dwErr = ERROR_GEN_FAILURE;
+
+ /*
+ * Register the control handler function for the service and report to SCM.
+ */
+ Assert(g_u32SupSvcWinStatus == SERVICE_STOPPED);
+ g_hSupSvcWinCtrlHandler = RegisterServiceCtrlHandlerExW(g_bstrServiceName.raw(), autostartSvcWinServiceCtrlHandlerEx, NULL);
+ if (g_hSupSvcWinCtrlHandler)
+ {
+ if (autostartSvcWinSetServiceStatus(SERVICE_START_PENDING, 3000, NO_ERROR))
+ {
+ /*
+ * Create the event semaphore we'll be waiting on and
+ * then instantiate the actual services.
+ */
+ int rc = RTSemEventMultiCreate(&g_hSupSvcWinEvent);
+ if (RT_SUCCESS(rc))
+ {
+ /*
+ * Update the status and enter the work loop.
+ */
+ if (autostartSvcWinSetServiceStatus(SERVICE_RUNNING, 0, 0))
+ {
+ LogFlow(("autostartSvcWinServiceMain: calling autostartStartVMs\n"));
+
+ /* check if we should stopped already, e.g. windows shutdown */
+ rc = RTSemEventMultiWait(g_hSupSvcWinEvent, 1);
+ if (RT_FAILURE(rc))
+ {
+ /* No one signaled us to stop */
+ rc = autostartStartVMs();
+ }
+ autostartShutdown();
+ }
+ else
+ {
+ dwErr = GetLastError();
+ autostartSvcLogError("SetServiceStatus failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
+ }
+
+ RTSemEventMultiDestroy(g_hSupSvcWinEvent);
+ g_hSupSvcWinEvent = NIL_RTSEMEVENTMULTI;
+ }
+ else
+ autostartSvcLogError("RTSemEventMultiCreate failed, rc=%Rrc", rc);
+ }
+ else
+ {
+ dwErr = GetLastError();
+ autostartSvcLogError("SetServiceStatus failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
+ }
+ autostartSvcWinSetServiceStatus(SERVICE_STOPPED, 0, dwErr);
+ }
+ /* else error will be handled by the caller. */
+}
+
+
+/**
+ * Handle the 'runit' action.
+ *
+ * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
+ * @param argc The action argument count.
+ * @param argv The action argument vector.
+ */
+static RTEXITCODE autostartSvcWinRunIt(int argc, char **argv)
+{
+ int vrc;
+
+ LogFlowFuncEnter();
+
+ /*
+ * Init com here for first main thread initialization.
+ * Service main function called in another thread
+ * created by service manager.
+ */
+ HRESULT hrc = com::Initialize();
+# ifdef VBOX_WITH_XPCOM
+ if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
+ {
+ char szHome[RTPATH_MAX] = "";
+ com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
+ return RTMsgErrorExit(RTEXITCODE_FAILURE,
+ "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
+ }
+# endif
+ if (FAILED(hrc))
+ return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
+ /*
+ * Initialize release logging, do this early. This means command
+ * line options (like --logfile &c) can't be introduced to affect
+ * the log file parameters, but the user can't change them easily
+ * anyway and is better off using environment variables.
+ */
+ do
+ {
+ char szLogFile[RTPATH_MAX];
+ vrc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile),
+ /* :fCreateDir */ false);
+ if (RT_FAILURE(vrc))
+ {
+ autostartSvcLogError("Failed to get VirtualBox user home directory: %Rrc\n", vrc);
+ break;
+ }
+
+ if (!RTDirExists(szLogFile)) /* vbox user home dir */
+ {
+ autostartSvcLogError("%s doesn't exist\n", szLogFile);
+ break;
+ }
+
+ vrc = RTPathAppend(szLogFile, sizeof(szLogFile), "VBoxAutostart.log");
+ if (RT_FAILURE(vrc))
+ {
+ autostartSvcLogError( "Failed to construct release log file name: %Rrc\n", vrc);
+ break;
+ }
+
+ vrc = com::VBoxLogRelCreate(AUTOSTART_SERVICE_NAME,
+ szLogFile,
+ RTLOGFLAGS_PREFIX_THREAD
+ | RTLOGFLAGS_PREFIX_TIME_PROG,
+ "all",
+ "VBOXAUTOSTART_RELEASE_LOG",
+ RTLOGDEST_FILE,
+ UINT32_MAX /* cMaxEntriesPerGroup */,
+ g_cHistory,
+ g_uHistoryFileTime,
+ g_uHistoryFileSize,
+ NULL);
+ if (RT_FAILURE(vrc))
+ autostartSvcLogError("Failed to create release log file: %Rrc\n", vrc);
+ } while (0);
+
+ /*
+ * Parse the arguments.
+ */
+ static const RTGETOPTDEF s_aOptions[] =
+ {
+ /* Common options first. */
+ { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
+ { "--service", 's', RTGETOPT_REQ_STRING },
+ };
+
+ const char *pszServiceName = NULL;
+ int ch;
+ RTGETOPTUNION ValueUnion;
+ RTGETOPTSTATE GetState;
+ RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
+ while ((ch = RTGetOpt(&GetState, &ValueUnion)))
+ {
+ switch (ch)
+ {
+ /* Common options first. */
+ case 'v':
+ g_cVerbosity++;
+ break;
+ case 's':
+ pszServiceName = ValueUnion.psz;
+ try
+ {
+ g_bstrServiceName = com::Bstr(ValueUnion.psz);
+ }
+ catch (...)
+ {
+ autostartSvcLogError("runit failed, service name is not valid UTF-8 string or out of memory");
+ return RTEXITCODE_FAILURE;
+ }
+ break;
+
+ default:
+ return autostartSvcDisplayGetOptError("runit", ch, &ValueUnion);
+ }
+ }
+
+ if (!pszServiceName)
+ {
+ autostartSvcLogError("runit failed, service name is missing");
+ return RTEXITCODE_SYNTAX;
+ }
+
+ autostartSvcLogInfo("Starting service %ls\n", g_bstrServiceName.raw());
+
+ /*
+ * Register the service with the service control manager
+ * and start dispatching requests from it (all done by the API).
+ */
+ SERVICE_TABLE_ENTRYW const s_aServiceStartTable[] =
+ {
+ { g_bstrServiceName.raw(), autostartSvcWinServiceMain },
+ { NULL, NULL}
+ };
+
+ if (StartServiceCtrlDispatcherW(&s_aServiceStartTable[0]))
+ {
+ LogFlowFuncLeave();
+ return RTEXITCODE_SUCCESS; /* told to quit, so quit. */
+ }
+
+ DWORD const dwErr = GetLastError();
+ switch (dwErr)
+ {
+ case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
+ autostartSvcLogWarning("Cannot run a service from the command line. Use the 'start' action to start it the right way.\n");
+ autostartSvcWinServiceMain(0 /* cArgs */, NULL /* papwszArgs */);
+ break;
+ default:
+ autostartSvcLogError("StartServiceCtrlDispatcher failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
+ break;
+ }
+
+ com::Shutdown();
+
+ return RTEXITCODE_FAILURE;
+}
+
+
+/**
+ * Show the version info.
+ *
+ * @returns RTEXITCODE_SUCCESS.
+ */
+static RTEXITCODE autostartSvcWinShowVersion(int argc, char **argv)
+{
+ /*
+ * Parse the arguments.
+ */
+ bool fBrief = false;
+ static const RTGETOPTDEF s_aOptions[] =
+ {
+ { "--brief", 'b', RTGETOPT_REQ_NOTHING }
+ };
+ int ch;
+ RTGETOPTUNION Value;
+ RTGETOPTSTATE GetState;
+ RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
+ while ((ch = RTGetOpt(&GetState, &Value)))
+ switch (ch)
+ {
+ case 'b': fBrief = true; break;
+ default: return autostartSvcDisplayGetOptError("version", ch, &Value);
+ }
+
+ /*
+ * Do the printing.
+ */
+ autostartSvcShowVersion(fBrief);
+
+ return RTEXITCODE_SUCCESS;
+}
+
+
+/**
+ * Show the usage help screen.
+ *
+ * @returns RTEXITCODE_SUCCESS.
+ */
+static RTEXITCODE autostartSvcWinShowHelp(void)
+{
+ autostartSvcShowHeader();
+
+ const char *pszExe = RTPathFilename(RTProcExecutablePath());
+
+ RTPrintf("Usage:\n"
+ "\n"
+ "%s [global-options] [command] [command-options]\n"
+ "\n"
+ "Global options:\n"
+ " -v\n"
+ " Increases the verbosity. Can be specified multiple times."
+ "\n\n"
+ "No command given:\n"
+ " Runs the service.\n"
+ "Options:\n"
+ " --service <name>\n"
+ " Specifies the service name to run.\n"
+ "\n"
+ "Command </help|help|-?|-h|--help> [...]\n"
+ " Displays this help screen.\n"
+ "\n"
+ "Command </version|version|-V|--version> [-brief]\n"
+ " Displays the version.\n"
+ "\n"
+ "Command </i|install|/RegServer> --user <username> --password-file <...>\n"
+ " Installs the service.\n"
+ "Options:\n"
+ " --user <username>\n"
+ " Specifies the user name the service should be installed for.\n"
+ " --password-file <path/to/file>\n"
+ " Specifies the file for user password to use for installation.\n"
+ "\n"
+ "Command </u|uninstall|delete|/UnregServer>\n"
+ " Uninstalls the service.\n"
+ " --user <username>\n"
+ " Specifies the user name the service should will be deleted for.\n",
+ pszExe);
+ return RTEXITCODE_SUCCESS;
+}
+
+
+/**
+ * VBoxAutostart main(), Windows edition.
+ *
+ *
+ * @returns 0 on success.
+ *
+ * @param argc Number of arguments in argv.
+ * @param argv Argument vector.
+ */
+int main(int argc, char **argv)
+{
+ /*
+ * Initialize the IPRT first of all.
+ */
+ int rc = RTR3InitExe(argc, &argv, 0);
+ if (RT_FAILURE(rc))
+ {
+ autostartSvcLogError("RTR3InitExe failed with rc=%Rrc", rc);
+ return RTEXITCODE_FAILURE;
+ }
+
+ /*
+ * Parse the initial arguments to determine the desired action.
+ */
+ enum
+ {
+ kAutoSvcAction_RunIt,
+
+ kAutoSvcAction_Create,
+ kAutoSvcAction_Delete,
+
+ kAutoSvcAction_Enable,
+ kAutoSvcAction_Disable,
+ kAutoSvcAction_QueryConfig,
+ kAutoSvcAction_QueryDescription,
+
+ kAutoSvcAction_Start,
+ kAutoSvcAction_Pause,
+ kAutoSvcAction_Continue,
+ kAutoSvcAction_Stop,
+ kAutoSvcAction_Interrogate,
+
+ kAutoSvcAction_End
+ } enmAction = kAutoSvcAction_RunIt;
+ int iArg = 1;
+ if (argc > 1)
+ {
+ if ( !stricmp(argv[iArg], "/RegServer")
+ || !stricmp(argv[iArg], "install")
+ || !stricmp(argv[iArg], "/i"))
+ enmAction = kAutoSvcAction_Create;
+ else if ( !stricmp(argv[iArg], "/UnregServer")
+ || !stricmp(argv[iArg], "/u")
+ || !stricmp(argv[iArg], "uninstall")
+ || !stricmp(argv[iArg], "delete"))
+ enmAction = kAutoSvcAction_Delete;
+
+ else if (!stricmp(argv[iArg], "enable"))
+ enmAction = kAutoSvcAction_Enable;
+ else if (!stricmp(argv[iArg], "disable"))
+ enmAction = kAutoSvcAction_Disable;
+ else if (!stricmp(argv[iArg], "qconfig"))
+ enmAction = kAutoSvcAction_QueryConfig;
+ else if (!stricmp(argv[iArg], "qdescription"))
+ enmAction = kAutoSvcAction_QueryDescription;
+
+ else if ( !stricmp(argv[iArg], "start")
+ || !stricmp(argv[iArg], "/t"))
+ enmAction = kAutoSvcAction_Start;
+ else if (!stricmp(argv[iArg], "pause"))
+ enmAction = kAutoSvcAction_Start;
+ else if (!stricmp(argv[iArg], "continue"))
+ enmAction = kAutoSvcAction_Continue;
+ else if (!stricmp(argv[iArg], "stop"))
+ enmAction = kAutoSvcAction_Stop;
+ else if (!stricmp(argv[iArg], "interrogate"))
+ enmAction = kAutoSvcAction_Interrogate;
+ else if ( !stricmp(argv[iArg], "help")
+ || !stricmp(argv[iArg], "?")
+ || !stricmp(argv[iArg], "/?")
+ || !stricmp(argv[iArg], "-?")
+ || !stricmp(argv[iArg], "/h")
+ || !stricmp(argv[iArg], "-h")
+ || !stricmp(argv[iArg], "/help")
+ || !stricmp(argv[iArg], "-help")
+ || !stricmp(argv[iArg], "--help"))
+ return autostartSvcWinShowHelp();
+ else if ( !stricmp(argv[iArg], "version")
+ || !stricmp(argv[iArg], "/ver")
+ || !stricmp(argv[iArg], "-V") /* Note: "-v" is used for specifying the verbosity. */
+ || !stricmp(argv[iArg], "/version")
+ || !stricmp(argv[iArg], "-version")
+ || !stricmp(argv[iArg], "--version"))
+ return autostartSvcWinShowVersion(argc - iArg - 1, argv + iArg + 1);
+ else
+ iArg--;
+ iArg++;
+ }
+
+ /*
+ * Dispatch it.
+ */
+ switch (enmAction)
+ {
+ case kAutoSvcAction_RunIt:
+ return autostartSvcWinRunIt(argc - iArg, argv + iArg);
+
+ case kAutoSvcAction_Create:
+ return autostartSvcWinCreate(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Delete:
+ return autostartSvcWinDelete(argc - iArg, argv + iArg);
+
+ case kAutoSvcAction_Enable:
+ return autostartSvcWinEnable(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Disable:
+ return autostartSvcWinDisable(argc - iArg, argv + iArg);
+ case kAutoSvcAction_QueryConfig:
+ return autostartSvcWinQueryConfig(argc - iArg, argv + iArg);
+ case kAutoSvcAction_QueryDescription:
+ return autostartSvcWinQueryDescription(argc - iArg, argv + iArg);
+
+ case kAutoSvcAction_Start:
+ return autostartSvcWinStart(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Pause:
+ return autostartSvcWinPause(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Continue:
+ return autostartSvcWinContinue(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Stop:
+ return autostartSvcWinStop(argc - iArg, argv + iArg);
+ case kAutoSvcAction_Interrogate:
+ return autostartSvcWinInterrogate(argc - iArg, argv + iArg);
+
+ default:
+ AssertMsgFailed(("enmAction=%d\n", enmAction));
+ return RTEXITCODE_FAILURE;
+ }
+}
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h
new file mode 100644
index 00000000..e291182c
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h
@@ -0,0 +1,383 @@
+/* $Id: VBoxAutostart.h $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#ifndef VBOX_INCLUDED_SRC_VBoxAutostart_VBoxAutostart_h
+#define VBOX_INCLUDED_SRC_VBoxAutostart_VBoxAutostart_h
+#ifndef RT_WITHOUT_PRAGMA_ONCE
+# pragma once
+#endif
+
+/*******************************************************************************
+* Header Files *
+*******************************************************************************/
+#include <iprt/getopt.h>
+#include <iprt/types.h>
+
+#include <VBox/cdefs.h>
+#include <VBox/types.h>
+
+#include <VBox/com/com.h>
+#include <VBox/com/VirtualBox.h>
+
+/*******************************************************************************
+* Constants And Macros, Structures and Typedefs *
+*******************************************************************************/
+
+/**
+ * Config AST node types.
+ */
+typedef enum CFGASTNODETYPE
+{
+ /** Invalid. */
+ CFGASTNODETYPE_INVALID = 0,
+ /** Key/Value pair. */
+ CFGASTNODETYPE_KEYVALUE,
+ /** Compound type. */
+ CFGASTNODETYPE_COMPOUND,
+ /** List type. */
+ CFGASTNODETYPE_LIST,
+ /** 32bit hack. */
+ CFGASTNODETYPE_32BIT_HACK = 0x7fffffff
+} CFGASTNODETYPE;
+/** Pointer to a config AST node type. */
+typedef CFGASTNODETYPE *PCFGASTNODETYPE;
+/** Pointer to a const config AST node type. */
+typedef const CFGASTNODETYPE *PCCFGASTNODETYPE;
+
+/**
+ * Config AST.
+ */
+typedef struct CFGAST
+{
+ /** AST node type. */
+ CFGASTNODETYPE enmType;
+ /** Key or scope id. */
+ char *pszKey;
+ /** Type dependent data. */
+ union
+ {
+ /** Key value pair. */
+ struct
+ {
+ /** Number of characters in the value - excluding terminator. */
+ size_t cchValue;
+ /** Value string - variable in size. */
+ char aszValue[1];
+ } KeyValue;
+ /** Compound type. */
+ struct
+ {
+ /** Number of AST node entries in the array. */
+ unsigned cAstNodes;
+ /** AST node array - variable in size. */
+ struct CFGAST *apAstNodes[1];
+ } Compound;
+ /** List type. */
+ struct
+ {
+ /** Number of entries in the list. */
+ unsigned cListEntries;
+ /** Array of list entries - variable in size. */
+ char *apszEntries[1];
+ } List;
+ } u;
+} CFGAST, *PCFGAST;
+
+/** Flag whether we are in verbose logging mode. */
+extern bool g_fVerbose;
+/** Handle to the VirtualBox interface. */
+extern ComPtr<IVirtualBox> g_pVirtualBox;
+/** Handle to the session interface. */
+extern ComPtr<ISession> g_pSession;
+/** handle to the VirtualBox interface. */
+extern ComPtr<IVirtualBoxClient> g_pVirtualBoxClient;
+/**
+ * System log type.
+ */
+typedef enum AUTOSTARTLOGTYPE
+{
+ /** Invalid log type. */
+ AUTOSTARTLOGTYPE_INVALID = 0,
+ /** Log info message. */
+ AUTOSTARTLOGTYPE_INFO,
+ /** Log error message. */
+ AUTOSTARTLOGTYPE_ERROR,
+ /** Log warning message. */
+ AUTOSTARTLOGTYPE_WARNING,
+ /** Log verbose message, only if verbose mode is activated. */
+ AUTOSTARTLOGTYPE_VERBOSE,
+ /** Famous 32bit hack. */
+ AUTOSTARTLOGTYPE_32BIT_HACK = 0x7fffffff
+} AUTOSTARTLOGTYPE;
+
+/**
+ * Prints the service header header (product name, version, ++) to stdout.
+ */
+DECLHIDDEN(void) autostartSvcShowHeader(void);
+
+/**
+ * Prints the service version information header to stdout.
+ *
+ * @param fBrief Whether to show brief information or not.
+ */
+DECLHIDDEN(void) autostartSvcShowVersion(bool fBrief);
+
+/**
+ * Log messages to the system and release log.
+ *
+ * @param pszMsg Message to log.
+ * @param enmLogType Log type to use.
+ */
+DECLHIDDEN(void) autostartSvcOsLogStr(const char *pszMsg, AUTOSTARTLOGTYPE enmLogType);
+
+/**
+ * Print out progress on the console.
+ *
+ * This runs the main event queue every now and then to prevent piling up
+ * unhandled things (which doesn't cause real problems, just makes things
+ * react a little slower than in the ideal case).
+ */
+DECLHIDDEN(HRESULT) showProgress(ComPtr<IProgress> progress);
+
+/**
+ * Converts the machine state to a human readable string.
+ *
+ * @returns Pointer to the human readable state.
+ * @param enmMachineState Machine state to convert.
+ * @param fShort Flag whether to return a short form.
+ */
+DECLHIDDEN(const char *) machineStateToName(MachineState_T enmMachineState, bool fShort);
+
+/**
+ * Parse the given configuration file and return the interesting config parameters.
+ *
+ * @returns VBox status code.
+ * @param pszFilename The config file to parse.
+ * @param ppCfgAst Where to store the pointer to the root AST node on success.
+ */
+DECLHIDDEN(int) autostartParseConfig(const char *pszFilename, PCFGAST *ppCfgAst);
+
+/**
+ * Destroys the config AST and frees all resources.
+ *
+ * @param pCfgAst The config AST.
+ */
+DECLHIDDEN(void) autostartConfigAstDestroy(PCFGAST pCfgAst);
+
+/**
+ * Return the config AST node with the given name or NULL if it doesn't exist.
+ *
+ * @returns Matching config AST node for the given name or NULL if not found.
+ * @param pCfgAst The config ASt to search.
+ * @param pszName The name to search for.
+ */
+DECLHIDDEN(PCFGAST) autostartConfigAstGetByName(PCFGAST pCfgAst, const char *pszName);
+
+/**
+ * Main routine for the autostart daemon.
+ *
+ * @returns VBox status code.
+ * @param pCfgAst Config AST for the startup part of the autostart daemon.
+ */
+DECLHIDDEN(int) autostartStartMain(PCFGAST pCfgAst);
+
+/**
+ * Main routine for the autostart daemon when stopping virtual machines
+ * during system shutdown.
+ *
+ * @returns VBox status code.
+ * @param pCfgAst Config AST for the shutdown part of the autostart daemon.
+ */
+DECLHIDDEN(int) autostartStopMain(PCFGAST pCfgAst);
+
+/**
+ * Logs a verbose message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param cVerbosity Verbosity level when logging should happen.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va);
+
+/**
+ * Logs a verbose message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param cVerbosity Verbosity level when logging should happen.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...);
+
+/**
+ * Logs a warning message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va);
+
+/**
+ * Logs a warning message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...);
+
+/**
+ * Logs a info message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va);
+
+/**
+ * Logs a info message to the appropriate system log and stdout + release log (if configured).
+ *
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...);
+
+/**
+ * Logs the message to the appropriate system log and stderr + release log (if configured).
+ *
+ * In debug builds this will also put it in the debug log.
+ *
+ * @returns VBox status code.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(int) autostartSvcLogErrorV(const char *pszFormat, va_list va);
+
+/**
+ * Logs the message to the appropriate system log and stderr + release log (if configured).
+ *
+ * In debug builds this will also put it in the debug log.
+ *
+ * @returns VBox status code.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(int) autostartSvcLogError(const char *pszFormat, ...);
+
+/**
+ * Logs the message to the appropriate system log.
+ *
+ * In debug builds this will also put it in the debug log.
+ *
+ * @returns VBox status code specified by \a rc.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ *
+ * @note Convenience function to return directly with the specified \a rc.
+ */
+DECLHIDDEN(int) autostartSvcLogErrorRcV(int rc, const char *pszFormat, va_list va);
+
+/**
+ * Logs the error message to the appropriate system log.
+ *
+ * In debug builds this will also put it in the debug log.
+ *
+ * @returns VBox status code specified by \a rc.
+ * @param pszFormat The log string. No trailing newline.
+ * @param ... Format arguments.
+ *
+ * @note Convenience function to return directly with the specified \a rc.
+ */
+DECLHIDDEN(int) autostartSvcLogErrorRc(int rc, const char *pszFormat, ...);
+
+/**
+ * Deals with RTGetOpt failure, bitching in the system log.
+ *
+ * @returns VBox status code specified by \a rc.
+ * @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.
+ */
+DECLHIDDEN(int) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue);
+
+/**
+ * Bitch about too many arguments (after RTGetOpt stops) in the system log.
+ *
+ * @returns VERR_INVALID_PARAMETER
+ * @param pszAction The action name.
+ * @param argc The argument count.
+ * @param argv The argument vector.
+ * @param iArg The argument index.
+ */
+DECLHIDDEN(int) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg);
+
+/**
+ * Prints an error message to the screen.
+ *
+ * @returns RTEXITCODE
+ * @param pszFormat The message format string.
+ * @param va Format arguments.
+ */
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va);
+
+/**
+ * Prints an error message to the screen.
+ *
+ * @returns RTEXITCODE
+ * @param pszFormat The message format string.
+ * @param ... Format arguments.
+ */
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...);
+
+/**
+ * Deals with RTGetOpt failure, i.e. an syntax error.
+ *
+ * @returns RTEXITCODE_SYNTAX
+ * @param pszAction The action name.
+ * @param rc The RTGetOpt return value.
+ * @param pValue The value returned by RTGetOpt.
+ */
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue);
+
+/**
+ * Starts the autostart environment by initializing all needed (global) objects.
+ *
+ * @returns VBox status code.
+ *
+ * @note This currently does NOT support multiple instances, be aware of this!
+ */
+DECLHIDDEN(int) autostartSetup(void);
+
+/**
+ * Stops the autostart environment.
+ *
+ * @note This currently does NOT support multiple instances, be aware of this!
+ */
+DECLHIDDEN(void) autostartShutdown(void);
+
+#endif /* !VBOX_INCLUDED_SRC_VBoxAutostart_VBoxAutostart_h */
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostartCfg.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartCfg.cpp
new file mode 100644
index 00000000..da557186
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartCfg.cpp
@@ -0,0 +1,748 @@
+/* $Id: VBoxAutostartCfg.cpp $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service, configuration parser.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+
+/*********************************************************************************************************************************
+* Header Files *
+*********************************************************************************************************************************/
+#include <iprt/ctype.h>
+#include <iprt/err.h>
+#include <iprt/mem.h>
+#include <iprt/message.h>
+#include <iprt/process.h>
+#include <iprt/stream.h>
+#include <iprt/string.h>
+
+#include "VBoxAutostart.h"
+
+
+/*********************************************************************************************************************************
+* Constants And Macros, Structures and Typedefs *
+*********************************************************************************************************************************/
+
+/**
+ * Token type.
+ */
+typedef enum CFGTOKENTYPE
+{
+ /** Invalid token type. */
+ CFGTOKENTYPE_INVALID = 0,
+ /** Identifier. */
+ CFGTOKENTYPE_ID,
+ /** Comma. */
+ CFGTOKENTYPE_COMMA,
+ /** Equal sign. */
+ CFGTOKENTYPE_EQUAL,
+ /** Open curly brackets. */
+ CFGTOKENTYPE_CURLY_OPEN,
+ /** Closing curly brackets. */
+ CFGTOKENTYPE_CURLY_CLOSING,
+ /** End of file. */
+ CFGTOKENTYPE_EOF,
+ /** 32bit hack. */
+ CFGTOKENTYPE_32BIT_HACK = 0x7fffffff
+} CFGTOKENTYPE;
+/** Pointer to a token type. */
+typedef CFGTOKENTYPE *PCFGTOKENTYPE;
+/** Pointer to a const token type. */
+typedef const CFGTOKENTYPE *PCCFGTOKENTYPE;
+
+/**
+ * A token.
+ */
+typedef struct CFGTOKEN
+{
+ /** Type of the token. */
+ CFGTOKENTYPE enmType;
+ /** Line number of the token. */
+ unsigned iLine;
+ /** Starting character of the token in the stream. */
+ size_t cchStart;
+ /** Type dependen token data. */
+ union
+ {
+ /** Data for the ID type. */
+ struct
+ {
+ /** Size of the id in characters, excluding the \0 terminator. */
+ size_t cchToken;
+ /** Token data, variable size (given by cchToken member). */
+ char achToken[1];
+ } Id;
+ } u;
+} CFGTOKEN;
+/** Pointer to a token. */
+typedef CFGTOKEN *PCFGTOKEN;
+/** Pointer to a const token. */
+typedef const CFGTOKEN *PCCFGTOKEN;
+
+/**
+ * Tokenizer instance data for the config data.
+ */
+typedef struct CFGTOKENIZER
+{
+ /** Config file handle. */
+ PRTSTREAM hStrmConfig;
+ /** String buffer for the current line we are operating in. */
+ char *pszLine;
+ /** Size of the string buffer. */
+ size_t cbLine;
+ /** Current position in the line. */
+ char *pszLineCurr;
+ /** Current line in the config file. */
+ unsigned iLine;
+ /** Current character of the line. */
+ size_t cchCurr;
+ /** Flag whether the end of the config stream is reached. */
+ bool fEof;
+ /** Pointer to the next token in the stream (used to peek). */
+ PCFGTOKEN pTokenNext;
+} CFGTOKENIZER, *PCFGTOKENIZER;
+
+
+/*********************************************************************************************************************************
+* Internal Functions *
+*********************************************************************************************************************************/
+
+/**
+ * Free a config token.
+ *
+ * @param pCfgTokenizer The config tokenizer.
+ * @param pToken The token to free.
+ */
+static void autostartConfigTokenFree(PCFGTOKENIZER pCfgTokenizer, PCFGTOKEN pToken)
+{
+ NOREF(pCfgTokenizer);
+ RTMemFree(pToken);
+}
+
+/**
+ * Reads the next line from the config stream.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The config tokenizer.
+ */
+static int autostartConfigTokenizerReadNextLine(PCFGTOKENIZER pCfgTokenizer)
+{
+ int rc = VINF_SUCCESS;
+
+ if (pCfgTokenizer->fEof)
+ return VERR_EOF;
+
+ do
+ {
+ rc = RTStrmGetLine(pCfgTokenizer->hStrmConfig, pCfgTokenizer->pszLine,
+ pCfgTokenizer->cbLine);
+ if (rc == VERR_BUFFER_OVERFLOW)
+ {
+ char *pszTmp;
+
+ pCfgTokenizer->cbLine += 128;
+ pszTmp = (char *)RTMemRealloc(pCfgTokenizer->pszLine, pCfgTokenizer->cbLine);
+ if (pszTmp)
+ pCfgTokenizer->pszLine = pszTmp;
+ else
+ rc = VERR_NO_MEMORY;
+ }
+ } while (rc == VERR_BUFFER_OVERFLOW);
+
+ if ( RT_SUCCESS(rc)
+ || rc == VERR_EOF)
+ {
+ pCfgTokenizer->iLine++;
+ pCfgTokenizer->cchCurr = 1;
+ pCfgTokenizer->pszLineCurr = pCfgTokenizer->pszLine;
+ if (rc == VERR_EOF)
+ pCfgTokenizer->fEof = true;
+ }
+
+ return rc;
+}
+
+/**
+ * Get the next token from the config stream and create a token structure.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The config tokenizer data.
+ * @param pCfgTokenUse Allocated token structure to use or NULL to allocate
+ * a new one. It will bee freed if an error is encountered.
+ * @param ppCfgToken Where to store the pointer to the next token on success.
+ */
+static int autostartConfigTokenizerCreateToken(PCFGTOKENIZER pCfgTokenizer,
+ PCFGTOKEN pCfgTokenUse, PCFGTOKEN *ppCfgToken)
+{
+ const char *pszToken = NULL;
+ size_t cchToken = 1;
+ size_t cchAdvance = 0;
+ CFGTOKENTYPE enmType = CFGTOKENTYPE_INVALID;
+ int rc = VINF_SUCCESS;
+
+ for (;;)
+ {
+ pszToken = pCfgTokenizer->pszLineCurr;
+
+ /* Skip all spaces. */
+ while (RT_C_IS_BLANK(*pszToken))
+ {
+ pszToken++;
+ cchAdvance++;
+ }
+
+ /* Check if we have to read a new line. */
+ if ( *pszToken == '\0'
+ || *pszToken == '#')
+ {
+ rc = autostartConfigTokenizerReadNextLine(pCfgTokenizer);
+ if (rc == VERR_EOF)
+ {
+ enmType = CFGTOKENTYPE_EOF;
+ rc = VINF_SUCCESS;
+ break;
+ }
+ else if (RT_FAILURE(rc))
+ break;
+ /* start from the beginning. */
+ cchAdvance = 0;
+ }
+ else if (*pszToken == '=')
+ {
+ enmType = CFGTOKENTYPE_EQUAL;
+ break;
+ }
+ else if (*pszToken == ',')
+ {
+ enmType = CFGTOKENTYPE_COMMA;
+ break;
+ }
+ else if (*pszToken == '{')
+ {
+ enmType = CFGTOKENTYPE_CURLY_OPEN;
+ break;
+ }
+ else if (*pszToken == '}')
+ {
+ enmType = CFGTOKENTYPE_CURLY_CLOSING;
+ break;
+ }
+ else
+ {
+ const char *pszTmp = pszToken;
+ cchToken = 0;
+ enmType = CFGTOKENTYPE_ID;
+
+ /* Get the complete token. */
+ while ( RT_C_IS_ALNUM(*pszTmp)
+ || *pszTmp == '_'
+ || *pszTmp == '.')
+ {
+ pszTmp++;
+ cchToken++;
+ }
+ break;
+ }
+ }
+
+ Assert(RT_FAILURE(rc) || enmType != CFGTOKENTYPE_INVALID);
+
+ if (RT_SUCCESS(rc))
+ {
+ /* Free the given token if it is an ID or the current one is an ID token. */
+ if ( pCfgTokenUse
+ && ( pCfgTokenUse->enmType == CFGTOKENTYPE_ID
+ || enmType == CFGTOKENTYPE_ID))
+ {
+ autostartConfigTokenFree(pCfgTokenizer, pCfgTokenUse);
+ pCfgTokenUse = NULL;
+ }
+
+ if (!pCfgTokenUse)
+ {
+ size_t cbToken = sizeof(CFGTOKEN);
+ if (enmType == CFGTOKENTYPE_ID)
+ cbToken += (cchToken + 1) * sizeof(char);
+
+ pCfgTokenUse = (PCFGTOKEN)RTMemAllocZ(cbToken);
+ if (!pCfgTokenUse)
+ rc = VERR_NO_MEMORY;
+ }
+
+ if (RT_SUCCESS(rc))
+ {
+ /* Copy token data. */
+ pCfgTokenUse->enmType = enmType;
+ pCfgTokenUse->cchStart = pCfgTokenizer->cchCurr;
+ pCfgTokenUse->iLine = pCfgTokenizer->iLine;
+ if (enmType == CFGTOKENTYPE_ID)
+ {
+ pCfgTokenUse->u.Id.cchToken = cchToken;
+ memcpy(pCfgTokenUse->u.Id.achToken, pszToken, cchToken);
+ }
+ }
+ else if (pCfgTokenUse)
+ autostartConfigTokenFree(pCfgTokenizer, pCfgTokenUse);
+
+ if (RT_SUCCESS(rc))
+ {
+ /* Set new position in config stream. */
+ pCfgTokenizer->pszLineCurr += cchToken + cchAdvance;
+ pCfgTokenizer->cchCurr += cchToken + cchAdvance;
+ *ppCfgToken = pCfgTokenUse;
+ }
+ }
+
+ return rc;
+}
+
+/**
+ * Destroys the given config tokenizer.
+ *
+ * @param pCfgTokenizer The config tokenizer to destroy.
+ */
+static void autostartConfigTokenizerDestroy(PCFGTOKENIZER pCfgTokenizer)
+{
+ if (pCfgTokenizer->pszLine)
+ RTMemFree(pCfgTokenizer->pszLine);
+ if (pCfgTokenizer->hStrmConfig)
+ RTStrmClose(pCfgTokenizer->hStrmConfig);
+ if (pCfgTokenizer->pTokenNext)
+ RTMemFree(pCfgTokenizer->pTokenNext);
+ RTMemFree(pCfgTokenizer);
+}
+
+/**
+ * Creates the config tokenizer from the given filename.
+ *
+ * @returns VBox status code.
+ * @param pszFilename Config filename.
+ * @param ppCfgTokenizer Where to store the pointer to the config tokenizer on
+ * success.
+ */
+static int autostartConfigTokenizerCreate(const char *pszFilename, PCFGTOKENIZER *ppCfgTokenizer)
+{
+ int rc = VINF_SUCCESS;
+ PCFGTOKENIZER pCfgTokenizer = (PCFGTOKENIZER)RTMemAllocZ(sizeof(CFGTOKENIZER));
+
+ if (pCfgTokenizer)
+ {
+ pCfgTokenizer->iLine = 0;
+ pCfgTokenizer->cbLine = 128;
+ pCfgTokenizer->pszLine = (char *)RTMemAllocZ(pCfgTokenizer->cbLine);
+ if (pCfgTokenizer->pszLine)
+ {
+ rc = RTStrmOpen(pszFilename, "r", &pCfgTokenizer->hStrmConfig);
+ if (RT_SUCCESS(rc))
+ {
+ rc = autostartConfigTokenizerReadNextLine(pCfgTokenizer);
+ if (RT_SUCCESS(rc))
+ rc = autostartConfigTokenizerCreateToken(pCfgTokenizer, NULL,
+ &pCfgTokenizer->pTokenNext);
+ }
+ }
+ else
+ rc = VERR_NO_MEMORY;
+ }
+ else
+ rc = VERR_NO_MEMORY;
+
+ if (RT_SUCCESS(rc))
+ *ppCfgTokenizer = pCfgTokenizer;
+ else if ( RT_FAILURE(rc)
+ && pCfgTokenizer)
+ autostartConfigTokenizerDestroy(pCfgTokenizer);
+
+ return rc;
+}
+
+/**
+ * Return the next token from the config stream.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The config tokenizer.
+ * @param ppCfgToken Where to store the next token.
+ */
+static int autostartConfigTokenizerGetNextToken(PCFGTOKENIZER pCfgTokenizer,
+ PCFGTOKEN *ppCfgToken)
+{
+ *ppCfgToken = pCfgTokenizer->pTokenNext;
+ return autostartConfigTokenizerCreateToken(pCfgTokenizer, NULL, &pCfgTokenizer->pTokenNext);
+}
+
+/**
+ * Returns a stringified version of the token type.
+ *
+ * @returns Stringified version of the token type.
+ * @param enmType Token type.
+ */
+static const char *autostartConfigTokenTypeToStr(CFGTOKENTYPE enmType)
+{
+ switch (enmType)
+ {
+ case CFGTOKENTYPE_COMMA:
+ return ",";
+ case CFGTOKENTYPE_EQUAL:
+ return "=";
+ case CFGTOKENTYPE_CURLY_OPEN:
+ return "{";
+ case CFGTOKENTYPE_CURLY_CLOSING:
+ return "}";
+ case CFGTOKENTYPE_EOF:
+ return "<EOF>";
+ case CFGTOKENTYPE_ID:
+ return "<Identifier>";
+ default:
+ AssertFailed();
+ return "<Invalid>";
+ }
+ /* not reached */
+}
+
+/**
+ * Returns a stringified version of the token.
+ *
+ * @returns Stringified version of the token type.
+ * @param pToken Token.
+ */
+static const char *autostartConfigTokenToString(PCFGTOKEN pToken)
+{
+ if (pToken->enmType == CFGTOKENTYPE_ID)
+ return pToken->u.Id.achToken;
+ else
+ return autostartConfigTokenTypeToStr(pToken->enmType);
+}
+
+/**
+ * Returns the length of the token in characters (without zero terminator).
+ *
+ * @returns Token length.
+ * @param pToken Token.
+ */
+static size_t autostartConfigTokenGetLength(PCFGTOKEN pToken)
+{
+ switch (pToken->enmType)
+ {
+ case CFGTOKENTYPE_COMMA:
+ case CFGTOKENTYPE_EQUAL:
+ case CFGTOKENTYPE_CURLY_OPEN:
+ case CFGTOKENTYPE_CURLY_CLOSING:
+ return 1;
+ case CFGTOKENTYPE_EOF:
+ return 0;
+ case CFGTOKENTYPE_ID:
+ return strlen(pToken->u.Id.achToken);
+ default:
+ AssertFailed();
+ return 0;
+ }
+ /* not reached */
+}
+
+/**
+ * Log unexpected token error.
+ *
+ * @returns VBox status code (VERR_INVALID_PARAMETER).
+ * @param pToken The token which caused the error.
+ * @param pszExpected String of the token which was expected.
+ */
+static int autostartConfigTokenizerMsgUnexpectedToken(PCFGTOKEN pToken, const char *pszExpected)
+{
+ return autostartSvcLogErrorRc(VERR_INVALID_PARAMETER, "Unexpected token '%s' at %d:%d.%d, expected '%s'",
+ autostartConfigTokenToString(pToken),
+ pToken->iLine, pToken->cchStart,
+ pToken->cchStart + autostartConfigTokenGetLength(pToken) - 1, pszExpected);
+}
+
+/**
+ * Verfies a token and consumes it.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The config tokenizer.
+ * @param pszTokenCheck The token to check for.
+ */
+static int autostartConfigTokenizerCheckAndConsume(PCFGTOKENIZER pCfgTokenizer, CFGTOKENTYPE enmType)
+{
+ int rc = VINF_SUCCESS;
+ PCFGTOKEN pCfgToken = NULL;
+
+ rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pCfgToken);
+ if (RT_SUCCESS(rc))
+ {
+ if (pCfgToken->enmType != enmType)
+ return autostartConfigTokenizerMsgUnexpectedToken(pCfgToken, autostartConfigTokenTypeToStr(enmType));
+
+ autostartConfigTokenFree(pCfgTokenizer, pCfgToken);
+ }
+ return rc;
+}
+
+/**
+ * Consumes the next token in the stream.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer Tokenizer instance data.
+ */
+static int autostartConfigTokenizerConsume(PCFGTOKENIZER pCfgTokenizer)
+{
+ int rc = VINF_SUCCESS;
+ PCFGTOKEN pCfgToken = NULL;
+
+ rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pCfgToken);
+ if (RT_SUCCESS(rc))
+ autostartConfigTokenFree(pCfgTokenizer, pCfgToken);
+
+ return rc;
+}
+
+/**
+ * Returns the start of the next token without consuming it.
+ *
+ * @returns The next token without consuming it.
+ * @param pCfgTokenizer Tokenizer instance data.
+ */
+DECLINLINE(PCFGTOKEN) autostartConfigTokenizerPeek(PCFGTOKENIZER pCfgTokenizer)
+{
+ return pCfgTokenizer->pTokenNext;
+}
+
+/**
+ * Check whether the next token is equal to the given one.
+ *
+ * @returns true if the next token in the stream is equal to the given one
+ * false otherwise.
+ * @param pszToken The token to check for.
+ */
+DECLINLINE(bool) autostartConfigTokenizerPeekIsEqual(PCFGTOKENIZER pCfgTokenizer, CFGTOKENTYPE enmType)
+{
+ PCFGTOKEN pToken = autostartConfigTokenizerPeek(pCfgTokenizer);
+ return pToken->enmType == enmType;
+}
+
+/**
+ * Parse a key value node and returns the AST.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The tokenizer for the config stream.
+ * @param pszKey The key for the pair.
+ * @param ppCfgAst Where to store the resulting AST on success.
+ */
+static int autostartConfigParseValue(PCFGTOKENIZER pCfgTokenizer, const char *pszKey,
+ PCFGAST *ppCfgAst)
+{
+ int rc = VINF_SUCCESS;
+ PCFGTOKEN pToken = NULL;
+
+ rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pToken);
+ if ( RT_SUCCESS(rc)
+ && pToken->enmType == CFGTOKENTYPE_ID)
+ {
+ PCFGAST pCfgAst = NULL;
+
+ pCfgAst = (PCFGAST)RTMemAllocZ(RT_UOFFSETOF_DYN(CFGAST, u.KeyValue.aszValue[pToken->u.Id.cchToken + 1]));
+ if (!pCfgAst)
+ return VERR_NO_MEMORY;
+
+ pCfgAst->enmType = CFGASTNODETYPE_KEYVALUE;
+ pCfgAst->pszKey = RTStrDup(pszKey);
+ if (!pCfgAst->pszKey)
+ {
+ RTMemFree(pCfgAst);
+ return VERR_NO_MEMORY;
+ }
+
+ memcpy(pCfgAst->u.KeyValue.aszValue, pToken->u.Id.achToken, pToken->u.Id.cchToken);
+ pCfgAst->u.KeyValue.cchValue = pToken->u.Id.cchToken;
+ *ppCfgAst = pCfgAst;
+ }
+ else
+ rc = autostartConfigTokenizerMsgUnexpectedToken(pToken, "non reserved token");
+
+ return rc;
+}
+
+/**
+ * Parses a compound node constructing the AST and returning it on success.
+ *
+ * @returns VBox status code.
+ * @param pCfgTokenizer The tokenizer for the config stream.
+ * @param pszScopeId The scope ID of the compound node.
+ * @param ppCfgAst Where to store the resulting AST on success.
+ */
+static int autostartConfigParseCompoundNode(PCFGTOKENIZER pCfgTokenizer, const char *pszScopeId,
+ PCFGAST *ppCfgAst)
+{
+ unsigned cAstNodesMax = 10;
+ PCFGAST pCfgAst = (PCFGAST)RTMemAllocZ(RT_UOFFSETOF_DYN(CFGAST, u.Compound.apAstNodes[cAstNodesMax]));
+ if (!pCfgAst)
+ return VERR_NO_MEMORY;
+
+ pCfgAst->enmType = CFGASTNODETYPE_COMPOUND;
+ pCfgAst->u.Compound.cAstNodes = 0;
+ pCfgAst->pszKey = RTStrDup(pszScopeId);
+ if (!pCfgAst->pszKey)
+ {
+ RTMemFree(pCfgAst);
+ return VERR_NO_MEMORY;
+ }
+
+ int rc = VINF_SUCCESS;
+ do
+ {
+ PCFGTOKEN pToken = NULL;
+ PCFGAST pAstNode = NULL;
+
+ if ( autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_CURLY_CLOSING)
+ || autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_EOF))
+ break;
+
+ rc = autostartConfigTokenizerGetNextToken(pCfgTokenizer, &pToken);
+ if ( RT_SUCCESS(rc)
+ && pToken->enmType == CFGTOKENTYPE_ID)
+ {
+ /* Next must be a = token in all cases at this place. */
+ rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_EQUAL);
+ if (RT_SUCCESS(rc))
+ {
+ /* Check whether this is a compound node. */
+ if (autostartConfigTokenizerPeekIsEqual(pCfgTokenizer, CFGTOKENTYPE_CURLY_OPEN))
+ {
+ rc = autostartConfigTokenizerConsume(pCfgTokenizer);
+ if (RT_SUCCESS(rc))
+ rc = autostartConfigParseCompoundNode(pCfgTokenizer, pToken->u.Id.achToken,
+ &pAstNode);
+
+ if (RT_SUCCESS(rc))
+ rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_CURLY_CLOSING);
+ }
+ else
+ rc = autostartConfigParseValue(pCfgTokenizer, pToken->u.Id.achToken,
+ &pAstNode);
+ }
+ }
+ else if (RT_SUCCESS(rc))
+ rc = autostartConfigTokenizerMsgUnexpectedToken(pToken, "non reserved token");
+
+ /* Add to the current compound node. */
+ if (RT_SUCCESS(rc))
+ {
+ if (pCfgAst->u.Compound.cAstNodes >= cAstNodesMax)
+ {
+ cAstNodesMax += 10;
+
+ PCFGAST pCfgAstNew = (PCFGAST)RTMemRealloc(pCfgAst, RT_UOFFSETOF_DYN(CFGAST, u.Compound.apAstNodes[cAstNodesMax]));
+ if (!pCfgAstNew)
+ rc = VERR_NO_MEMORY;
+ else
+ pCfgAst = pCfgAstNew;
+ }
+
+ if (RT_SUCCESS(rc))
+ {
+ pCfgAst->u.Compound.apAstNodes[pCfgAst->u.Compound.cAstNodes] = pAstNode;
+ pCfgAst->u.Compound.cAstNodes++;
+ }
+ }
+
+ autostartConfigTokenFree(pCfgTokenizer, pToken);
+
+ } while (RT_SUCCESS(rc));
+
+ if (RT_SUCCESS(rc))
+ *ppCfgAst = pCfgAst;
+ else
+ autostartConfigAstDestroy(pCfgAst);
+
+ return rc;
+}
+
+DECLHIDDEN(int) autostartParseConfig(const char *pszFilename, PCFGAST *ppCfgAst)
+{
+ PCFGTOKENIZER pCfgTokenizer = NULL;
+ int rc = VINF_SUCCESS;
+ PCFGAST pCfgAst = NULL;
+
+ AssertPtrReturn(pszFilename, VERR_INVALID_POINTER);
+ AssertPtrReturn(ppCfgAst, VERR_INVALID_POINTER);
+
+ rc = autostartConfigTokenizerCreate(pszFilename, &pCfgTokenizer);
+ if (RT_SUCCESS(rc))
+ {
+ rc = autostartConfigParseCompoundNode(pCfgTokenizer, "", &pCfgAst);
+ if (RT_SUCCESS(rc))
+ rc = autostartConfigTokenizerCheckAndConsume(pCfgTokenizer, CFGTOKENTYPE_EOF);
+ }
+
+ if (pCfgTokenizer)
+ autostartConfigTokenizerDestroy(pCfgTokenizer);
+
+ if (RT_SUCCESS(rc))
+ *ppCfgAst = pCfgAst;
+
+ return rc;
+}
+
+DECLHIDDEN(void) autostartConfigAstDestroy(PCFGAST pCfgAst)
+{
+ AssertPtrReturnVoid(pCfgAst);
+
+ switch (pCfgAst->enmType)
+ {
+ case CFGASTNODETYPE_KEYVALUE:
+ {
+ RTMemFree(pCfgAst);
+ break;
+ }
+ case CFGASTNODETYPE_COMPOUND:
+ {
+ for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
+ autostartConfigAstDestroy(pCfgAst->u.Compound.apAstNodes[i]);
+ RTMemFree(pCfgAst);
+ break;
+ }
+ case CFGASTNODETYPE_LIST:
+ RT_FALL_THROUGH();
+ default:
+ AssertMsgFailed(("Invalid AST node type %d\n", pCfgAst->enmType));
+ }
+}
+
+DECLHIDDEN(PCFGAST) autostartConfigAstGetByName(PCFGAST pCfgAst, const char *pszName)
+{
+ if (!pCfgAst)
+ return NULL;
+
+ AssertReturn(pCfgAst->enmType == CFGASTNODETYPE_COMPOUND, NULL);
+
+ for (unsigned i = 0; i < pCfgAst->u.Compound.cAstNodes; i++)
+ {
+ PCFGAST pNode = pCfgAst->u.Compound.apAstNodes[i];
+
+ if (!RTStrCmp(pNode->pszKey, pszName))
+ return pNode;
+ }
+
+ return NULL;
+}
+
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp
new file mode 100644
index 00000000..8a135233
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp
@@ -0,0 +1,222 @@
+/* $Id: VBoxAutostartStart.cpp $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <VBox/com/com.h>
+#include <VBox/com/string.h>
+#include <VBox/com/Guid.h>
+#include <VBox/com/array.h>
+#include <VBox/com/ErrorInfo.h>
+#include <VBox/com/errorprint.h>
+
+#include <iprt/errcore.h>
+#include <iprt/log.h>
+#include <iprt/message.h>
+#include <iprt/stream.h>
+#include <iprt/thread.h>
+
+#include <algorithm>
+#include <list>
+
+#include "VBoxAutostart.h"
+
+extern unsigned g_cVerbosity;
+
+using namespace com;
+
+/**
+ * VM list entry.
+ */
+typedef struct AUTOSTARTVM
+{
+ /** ID of the VM to start. */
+ Bstr strId;
+ /** Startup delay of the VM. */
+ ULONG uStartupDelay;
+} AUTOSTARTVM;
+
+static DECLCALLBACK(bool) autostartVMCmp(const AUTOSTARTVM &vm1, const AUTOSTARTVM &vm2)
+{
+ return vm1.uStartupDelay <= vm2.uStartupDelay;
+}
+
+DECLHIDDEN(int) autostartStartMain(PCFGAST pCfgAst)
+{
+ int vrc = VINF_SUCCESS;
+ std::list<AUTOSTARTVM> listVM;
+ uint32_t uStartupDelay = 0;
+
+ autostartSvcLogVerbose(1, "Starting machines ...\n");
+
+ pCfgAst = autostartConfigAstGetByName(pCfgAst, "startup_delay");
+ if (pCfgAst)
+ {
+ if (pCfgAst->enmType == CFGASTNODETYPE_KEYVALUE)
+ {
+ vrc = RTStrToUInt32Full(pCfgAst->u.KeyValue.aszValue, 10, &uStartupDelay);
+ if (RT_FAILURE(vrc))
+ return autostartSvcLogErrorRc(vrc, "'startup_delay' must be an unsigned number");
+ }
+ }
+
+ if (uStartupDelay)
+ {
+ autostartSvcLogVerbose(1, "Delaying start for %RU32 seconds ...\n", uStartupDelay);
+ vrc = RTThreadSleep(uStartupDelay * 1000);
+ }
+
+ if (vrc == VERR_INTERRUPTED)
+ return VINF_SUCCESS;
+
+ /*
+ * Build a list of all VMs we need to autostart first, apply the overrides
+ * from the configuration and start the VMs afterwards.
+ */
+ com::SafeIfaceArray<IMachine> machines;
+ HRESULT hrc = g_pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
+ if (SUCCEEDED(hrc))
+ {
+ /*
+ * Iterate through the collection
+ */
+ for (size_t i = 0; i < machines.size(); ++i)
+ {
+ if (machines[i])
+ {
+ Bstr strName;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Name)(strName.asOutParam()));
+
+ BOOL fAccessible;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Accessible)(&fAccessible));
+ if (!fAccessible)
+ {
+ autostartSvcLogVerbose(1, "Machine '%ls' is not accessible, skipping\n", strName.raw());
+ continue;
+ }
+
+ AUTOSTARTVM autostartVM;
+
+ BOOL fAutostart;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostartEnabled)(&fAutostart));
+ if (fAutostart)
+ {
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Id)(autostartVM.strId.asOutParam()));
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostartDelay)(&autostartVM.uStartupDelay));
+
+ listVM.push_back(autostartVM);
+ }
+
+ autostartSvcLogVerbose(1, "Machine '%ls': Autostart is %s (startup delay is %RU32 seconds)\n",
+ strName.raw(), fAutostart ? "enabled" : "disabled",
+ fAutostart ? autostartVM.uStartupDelay : 0);
+ }
+ }
+
+ /**
+ * @todo r=uwe I'm not reindenting this whole burnt offering
+ * to mistinterpreted Dijkstra's "single exit" commandment
+ * just to add this log, hence a bit of duplicate logic here.
+ */
+ if (SUCCEEDED(hrc))
+ {
+ if (machines.size() == 0)
+ autostartSvcLogWarning("No virtual machines found.\n"
+ "This either could be a configuration problem (access rights), "
+ "or there are no VMs configured yet.");
+ else if (listVM.empty())
+ autostartSvcLogWarning("No virtual machines configured for autostart.\n"
+ "Please consult the manual about how to enable auto starting VMs.\n");
+ }
+ else
+ autostartSvcLogError("Enumerating virtual machines failed with %Rhrc\n", hrc);
+
+ if ( SUCCEEDED(hrc)
+ && !listVM.empty())
+ {
+ ULONG uDelayCur = 0;
+
+ /* Sort by startup delay and apply base override. */
+ listVM.sort(autostartVMCmp);
+
+ std::list<AUTOSTARTVM>::iterator it;
+ for (it = listVM.begin(); it != listVM.end(); ++it)
+ {
+ ComPtr<IMachine> machine;
+ ComPtr<IProgress> progress;
+
+ CHECK_ERROR_BREAK(g_pVirtualBox, FindMachine((*it).strId.raw(), machine.asOutParam()));
+
+ Bstr strName;
+ CHECK_ERROR_BREAK(machine, COMGETTER(Name)(strName.asOutParam()));
+
+ if ((*it).uStartupDelay > uDelayCur)
+ {
+ autostartSvcLogVerbose(1, "Waiting for %ul seconds before starting machine '%s' ...\n",
+ (*it).uStartupDelay - uDelayCur, strName.raw());
+ RTThreadSleep(((*it).uStartupDelay - uDelayCur) * 1000);
+ uDelayCur = (*it).uStartupDelay;
+ }
+
+ CHECK_ERROR_BREAK(machine, LaunchVMProcess(g_pSession, Bstr("headless").raw(),
+ ComSafeArrayNullInParam(), progress.asOutParam()));
+ if (SUCCEEDED(hrc) && !progress.isNull())
+ {
+ autostartSvcLogVerbose(1, "Waiting for machine '%ls' to power on ...\n", strName.raw());
+ CHECK_ERROR(progress, WaitForCompletion(-1));
+ if (SUCCEEDED(hrc))
+ {
+ BOOL completed = true;
+ CHECK_ERROR(progress, COMGETTER(Completed)(&completed));
+ if (SUCCEEDED(hrc))
+ {
+ ASSERT(completed);
+
+ LONG iRc;
+ CHECK_ERROR(progress, COMGETTER(ResultCode)(&iRc));
+ if (SUCCEEDED(hrc))
+ {
+ if (FAILED(iRc))
+ {
+ ProgressErrorInfo info(progress);
+ com::GluePrintErrorInfo(info);
+ }
+ else
+ autostartSvcLogVerbose(1, "Machine '%ls' has been successfully started.\n", strName.raw());
+ }
+ }
+ }
+ }
+ SessionState_T enmSessionState;
+ CHECK_ERROR(g_pSession, COMGETTER(State)(&enmSessionState));
+ if (SUCCEEDED(hrc) && enmSessionState == SessionState_Locked)
+ g_pSession->UnlockMachine();
+ }
+ }
+ }
+
+ return vrc;
+}
+
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp
new file mode 100644
index 00000000..fb397fae
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStop.cpp
@@ -0,0 +1,267 @@
+/* $Id: VBoxAutostartStop.cpp $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service, stop machines during system shutdown.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <iprt/assert.h>
+#include <iprt/log.h>
+#include <iprt/message.h>
+#include <iprt/stream.h>
+#include <iprt/thread.h>
+#include <iprt/time.h>
+
+#include <VBox/com/com.h>
+#include <VBox/com/string.h>
+#include <VBox/com/Guid.h>
+#include <VBox/com/array.h>
+#include <VBox/com/ErrorInfo.h>
+#include <VBox/com/errorprint.h>
+
+#include <list>
+
+#include "VBoxAutostart.h"
+
+using namespace com;
+
+/**
+ * VM list entry.
+ */
+typedef struct AUTOSTOPVM
+{
+ /** ID of the VM to start. */
+ Bstr strId;
+ /** Action to do with the VM. */
+ AutostopType_T enmAutostopType;
+} AUTOSTOPVM;
+
+static HRESULT autostartSaveVMState(ComPtr<IConsole> &console)
+{
+ HRESULT hrc = S_OK;
+ ComPtr<IMachine> machine;
+ ComPtr<IProgress> progress;
+
+ do
+ {
+ /* first pause so we don't trigger a live save which needs more time/resources */
+ bool fPaused = false;
+ hrc = console->Pause();
+ if (FAILED(hrc))
+ {
+ bool fError = true;
+ if (hrc == VBOX_E_INVALID_VM_STATE)
+ {
+ /* check if we are already paused */
+ MachineState_T machineState;
+ CHECK_ERROR_BREAK(console, COMGETTER(State)(&machineState));
+ /* the error code was lost by the previous instruction */
+ hrc = VBOX_E_INVALID_VM_STATE;
+ if (machineState != MachineState_Paused)
+ {
+ RTMsgError("Machine in invalid state %d -- %s\n",
+ machineState, machineStateToName(machineState, false));
+ }
+ else
+ {
+ fError = false;
+ fPaused = true;
+ }
+ }
+ if (fError)
+ break;
+ }
+
+ CHECK_ERROR(console, COMGETTER(Machine)(machine.asOutParam()));
+ CHECK_ERROR(machine, SaveState(progress.asOutParam()));
+ if (FAILED(hrc))
+ {
+ if (!fPaused)
+ console->Resume();
+ break;
+ }
+
+ hrc = showProgress(progress);
+ CHECK_PROGRESS_ERROR(progress, ("Failed to save machine state"));
+ if (FAILED(hrc))
+ {
+ if (!fPaused)
+ console->Resume();
+ }
+ } while (0);
+
+ return hrc;
+}
+
+DECLHIDDEN(int) autostartStopMain(PCFGAST pCfgAst)
+{
+ RT_NOREF(pCfgAst);
+ std::list<AUTOSTOPVM> listVM;
+
+ autostartSvcLogVerbose(1, "Stopping machines ...\n");
+
+ /*
+ * Build a list of all VMs we need to autostop first, apply the overrides
+ * from the configuration and start the VMs afterwards.
+ */
+ com::SafeIfaceArray<IMachine> machines;
+ HRESULT hrc = g_pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
+ if (SUCCEEDED(hrc))
+ {
+ /*
+ * Iterate through the collection and construct a list of machines
+ * we have to check.
+ */
+ for (size_t i = 0; i < machines.size(); ++i)
+ {
+ if (machines[i])
+ {
+ Bstr strName;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Name)(strName.asOutParam()));
+
+ BOOL fAccessible;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Accessible)(&fAccessible));
+ if (!fAccessible)
+ {
+ autostartSvcLogVerbose(1, "Machine '%ls' is not accessible, skipping\n", strName.raw());
+ continue;
+ }
+
+ AUTOSTOPVM autostopVM;
+
+ AutostopType_T enmAutostopType;
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostopType)(&enmAutostopType));
+ if (enmAutostopType != AutostopType_Disabled)
+ {
+ CHECK_ERROR_BREAK(machines[i], COMGETTER(Id)(autostopVM.strId.asOutParam()));
+ autostopVM.enmAutostopType = enmAutostopType;
+
+ listVM.push_back(autostopVM);
+ }
+
+ autostartSvcLogVerbose(1, "Machine '%ls': Autostop type is %#x\n",
+ strName.raw(), autostopVM.enmAutostopType);
+ }
+ }
+
+ if ( SUCCEEDED(hrc)
+ && !listVM.empty())
+ {
+ std::list<AUTOSTOPVM>::iterator it;
+ for (it = listVM.begin(); it != listVM.end(); ++it)
+ {
+ MachineState_T enmMachineState;
+ ComPtr<IMachine> machine;
+
+ CHECK_ERROR_BREAK(g_pVirtualBox, FindMachine((*it).strId.raw(),
+ machine.asOutParam()));
+
+ Bstr strName;
+ CHECK_ERROR_BREAK(machine, COMGETTER(Name)(strName.asOutParam()));
+
+ CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
+
+ /* Wait until the VM changes from a transient state back. */
+ while ( enmMachineState >= MachineState_FirstTransient
+ && enmMachineState <= MachineState_LastTransient)
+ {
+ RTThreadSleep(1000);
+ CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
+ }
+
+ /* Only power off running machines. */
+ if ( enmMachineState == MachineState_Running
+ || enmMachineState == MachineState_Paused)
+ {
+ ComPtr<IConsole> console;
+ ComPtr<IProgress> progress;
+
+ /* open a session for the VM */
+ CHECK_ERROR_BREAK(machine, LockMachine(g_pSession, LockType_Shared));
+
+ /* get the associated console */
+ CHECK_ERROR_BREAK(g_pSession, COMGETTER(Console)(console.asOutParam()));
+
+ switch ((*it).enmAutostopType)
+ {
+ case AutostopType_SaveState:
+ {
+ hrc = autostartSaveVMState(console);
+ break;
+ }
+ case AutostopType_PowerOff:
+ {
+ CHECK_ERROR_BREAK(console, PowerDown(progress.asOutParam()));
+
+ hrc = showProgress(progress);
+ CHECK_PROGRESS_ERROR(progress, ("Failed to powering off machine '%ls'", strName.raw()));
+ if (FAILED(hrc))
+ autostartSvcLogError("Powering off machine '%ls' failed with %Rhrc\n", strName.raw(), hrc);
+ break;
+ }
+ case AutostopType_AcpiShutdown:
+ {
+ BOOL fGuestEnteredACPI = false;
+ CHECK_ERROR_BREAK(console, GetGuestEnteredACPIMode(&fGuestEnteredACPI));
+ if ( fGuestEnteredACPI
+ && enmMachineState == MachineState_Running)
+ {
+ CHECK_ERROR_BREAK(console, PowerButton());
+
+ autostartSvcLogVerbose(1, "Waiting for machine '%ls' to power off...\n", strName.raw());
+
+ uint64_t const tsStartMs = RTTimeMilliTS();
+ RTMSINTERVAL const msTimeout = RT_MS_5MIN; /* Should be enough time, shouldn't it? */
+
+ while (RTTimeMilliTS() - tsStartMs <= msTimeout)
+ {
+ CHECK_ERROR_BREAK(machine, COMGETTER(State)(&enmMachineState));
+ if (enmMachineState != MachineState_Running)
+ break;
+ RTThreadSleep(RT_MS_1SEC);
+ }
+
+ if (RTTimeMilliTS() - tsStartMs > msTimeout)
+ autostartSvcLogWarning("Machine '%ls' did not power off via ACPI within time\n", strName.raw());
+ }
+ else
+ {
+ /* Use save state instead and log this to the console. */
+ autostartSvcLogWarning("The guest of machine '%ls' does not support ACPI shutdown or is currently paused, saving state...\n",
+ strName.raw());
+ hrc = autostartSaveVMState(console);
+ }
+ break;
+ }
+ default:
+ autostartSvcLogWarning("Unknown autostop type for machine '%ls', skipping\n", strName.raw());
+ }
+ g_pSession->UnlockMachine();
+ }
+ }
+ }
+ }
+
+ return VINF_SUCCESS; /** @todo r=andy Report back the overall status here. */
+}
+
diff --git a/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp
new file mode 100644
index 00000000..a18526ad
--- /dev/null
+++ b/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp
@@ -0,0 +1,341 @@
+/* $Id: VBoxAutostartUtils.cpp $ */
+/** @file
+ * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
+ * Utils used by the windows and posix frontends.
+ */
+
+/*
+ * Copyright (C) 2012-2023 Oracle and/or its affiliates.
+ *
+ * This file is part of VirtualBox base platform packages, as
+ * available from https://www.virtualbox.org.
+ *
+ * This program is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU General Public License
+ * as published by the Free Software Foundation, in version 3 of the
+ * License.
+ *
+ * This program is distributed in the hope that it will be useful, but
+ * WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, see <https://www.gnu.org/licenses>.
+ *
+ * SPDX-License-Identifier: GPL-3.0-only
+ */
+
+#include <VBox/com/com.h>
+#include <VBox/com/string.h>
+#include <VBox/com/Guid.h>
+#include <VBox/com/array.h>
+#include <VBox/com/ErrorInfo.h>
+#include <VBox/com/errorprint.h>
+
+#include <VBox/err.h>
+#include <VBox/version.h>
+
+#include <iprt/buildconfig.h>
+#include <iprt/message.h>
+#include <iprt/thread.h>
+#include <iprt/stream.h>
+#include <iprt/log.h>
+#include <iprt/path.h> /* RTPATH_MAX */
+
+#include "VBoxAutostart.h"
+
+using namespace com;
+
+extern unsigned g_cVerbosity;
+
+DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
+{
+ switch (machineState)
+ {
+ case MachineState_PoweredOff:
+ return fShort ? "poweroff" : "powered off";
+ case MachineState_Saved:
+ return "saved";
+ case MachineState_Teleported:
+ return "teleported";
+ case MachineState_Aborted:
+ return "aborted";
+ case MachineState_AbortedSaved:
+ return "aborted-saved";
+ case MachineState_Running:
+ return "running";
+ case MachineState_Paused:
+ return "paused";
+ case MachineState_Stuck:
+ return fShort ? "gurumeditation" : "guru meditation";
+ case MachineState_Teleporting:
+ return "teleporting";
+ case MachineState_LiveSnapshotting:
+ return fShort ? "livesnapshotting" : "live snapshotting";
+ case MachineState_Starting:
+ return "starting";
+ case MachineState_Stopping:
+ return "stopping";
+ case MachineState_Saving:
+ return "saving";
+ case MachineState_Restoring:
+ return "restoring";
+ case MachineState_TeleportingPausedVM:
+ return fShort ? "teleportingpausedvm" : "teleporting paused vm";
+ case MachineState_TeleportingIn:
+ return fShort ? "teleportingin" : "teleporting (incoming)";
+ case MachineState_DeletingSnapshotOnline:
+ return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
+ case MachineState_DeletingSnapshotPaused:
+ return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
+ case MachineState_OnlineSnapshotting:
+ return fShort ? "onlinesnapshotting" : "online snapshotting";
+ case MachineState_RestoringSnapshot:
+ return fShort ? "restoringsnapshot" : "restoring snapshot";
+ case MachineState_DeletingSnapshot:
+ return fShort ? "deletingsnapshot" : "deleting snapshot";
+ case MachineState_SettingUp:
+ return fShort ? "settingup" : "setting up";
+ case MachineState_Snapshotting:
+ return "snapshotting";
+ default:
+ break;
+ }
+ return "unknown";
+}
+
+DECLHIDDEN(void) autostartSvcShowHeader(void)
+{
+ RTPrintf(VBOX_PRODUCT " VirtualBox Autostart Service Version " VBOX_VERSION_STRING " - r%s\n"
+ "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n", RTBldCfgRevisionStr());
+}
+
+DECLHIDDEN(void) autostartSvcShowVersion(bool fBrief)
+{
+ if (fBrief)
+ RTPrintf("%s\n", VBOX_VERSION_STRING);
+ else
+ autostartSvcShowHeader();
+}
+
+DECLHIDDEN(int) autostartSvcLogErrorV(const char *pszFormat, va_list va)
+{
+ AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
+
+ char *pszMsg = NULL;
+ if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
+ {
+ autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
+ RTStrFree(pszMsg);
+ return VINF_SUCCESS;
+ }
+
+ return VERR_BUFFER_OVERFLOW;
+}
+
+DECLHIDDEN(int) autostartSvcLogError(const char *pszFormat, ...)
+{
+ AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
+
+ va_list va;
+ va_start(va, pszFormat);
+ int rc = autostartSvcLogErrorV(pszFormat, va);
+ va_end(va);
+
+ return rc;
+}
+
+DECLHIDDEN(int) autostartSvcLogErrorRcV(int rc, const char *pszFormat, va_list va)
+{
+ AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
+
+ int rc2 = autostartSvcLogErrorV(pszFormat, va);
+ if (RT_SUCCESS(rc2))
+ return rc; /* Return handed-in rc. */
+ return rc2;
+}
+
+DECLHIDDEN(int) autostartSvcLogErrorRc(int rc, const char *pszFormat, ...)
+{
+ AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
+
+ va_list va;
+ va_start(va, pszFormat);
+ int rc2 = autostartSvcLogErrorRcV(rc, pszFormat, va);
+ va_end(va);
+ return rc2;
+}
+
+DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va)
+{
+ AssertPtrReturnVoid(pszFormat);
+
+ if (g_cVerbosity < cVerbosity)
+ return;
+
+ char *pszMsg = NULL;
+ if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
+ {
+ autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
+ RTStrFree(pszMsg);
+ }
+}
+
+DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...)
+{
+ va_list va;
+ va_start(va, pszFormat);
+ autostartSvcLogVerboseV(cVerbosity, pszFormat, va);
+ va_end(va);
+}
+
+DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
+{
+ AssertPtrReturnVoid(pszFormat);
+
+ char *pszMsg = NULL;
+ if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
+ {
+ autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
+ RTStrFree(pszMsg);
+ }
+}
+
+DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
+{
+ va_list va;
+ va_start(va, pszFormat);
+ autostartSvcLogWarningV(pszFormat, va);
+ va_end(va);
+}
+
+DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
+{
+ va_list va;
+ va_start(va, pszFormat);
+ autostartSvcLogInfoV(pszFormat, va);
+ va_end(va);
+}
+
+DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
+{
+ AssertPtrReturnVoid(pszFormat);
+
+ char *pszMsg = NULL;
+ if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
+ {
+ autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
+ RTStrFree(pszMsg);
+ }
+}
+
+DECLHIDDEN(int) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
+{
+ RT_NOREF(pValue);
+ autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s", pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
+ return RTEXITCODE_SYNTAX;
+}
+
+DECLHIDDEN(int) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
+{
+ AssertReturn(iArg < argc, RTEXITCODE_FAILURE);
+ autostartSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
+ for ( ; iArg < argc; iArg++)
+ LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
+ return VERR_INVALID_PARAMETER;
+}
+
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
+{
+ RTStrmPrintf(g_pStdErr, "Error: ");
+ RTStrmPrintfV(g_pStdErr, pszFormat, va);
+ Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
+ return RTEXITCODE_FAILURE;
+}
+
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...)
+{
+ va_list va;
+ va_start(va, pszFormat);
+ autostartSvcDisplayErrorV(pszFormat, va);
+ va_end(va);
+ return RTEXITCODE_FAILURE;
+}
+
+DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue)
+{
+ char szMsg[4096];
+ RTGetOptFormatError(szMsg, sizeof(szMsg), rc, pValue);
+ autostartSvcDisplayError("%s - %s", pszAction, szMsg);
+ return RTEXITCODE_SYNTAX;
+}
+
+DECLHIDDEN(int) autostartSetup(void)
+{
+ autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
+
+ /*
+ * Initialize COM.
+ */
+ using namespace com;
+ HRESULT hrc = com::Initialize();
+# ifdef VBOX_WITH_XPCOM
+ if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
+ {
+ char szHome[RTPATH_MAX] = "";
+ com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
+ autostartSvcLogError("Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
+ return VERR_COM_FILE_ERROR;
+ }
+# endif
+ if (FAILED(hrc))
+ {
+ autostartSvcLogError("Failed to initialize COM (%Rhrc)!", hrc);
+ return VERR_COM_UNEXPECTED;
+ }
+
+ hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
+ if (FAILED(hrc))
+ {
+ RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
+ com::ErrorInfo info;
+ if (!info.isFullAvailable() && !info.isBasicAvailable())
+ {
+ com::GluePrintRCMessage(hrc);
+ autostartSvcLogError("Most likely, the VirtualBox COM server is not running or failed to start.");
+ }
+ else
+ com::GluePrintErrorInfo(info);
+ return VERR_COM_UNEXPECTED;
+ }
+
+ /*
+ * Setup VirtualBox + session interfaces.
+ */
+ hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
+ if (SUCCEEDED(hrc))
+ {
+ hrc = g_pSession.createInprocObject(CLSID_Session);
+ if (FAILED(hrc))
+ autostartSvcLogError("Failed to create a session object (rc=%Rhrc)!", hrc);
+ }
+ else
+ autostartSvcLogError("Failed to get VirtualBox object (rc=%Rhrc)!", hrc);
+
+ if (FAILED(hrc))
+ return VERR_COM_OBJECT_NOT_FOUND;
+
+ return VINF_SUCCESS;
+}
+
+DECLHIDDEN(void) autostartShutdown(void)
+{
+ autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
+
+ g_pSession.setNull();
+ g_pVirtualBox.setNull();
+ g_pVirtualBoxClient.setNull();
+ com::Shutdown();
+}
+