1 | /* $Id: VBoxAutostart.cpp 42527 2012-08-02 11:01:56Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxAutostart - VirtualBox Autostart service.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2012 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*******************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *******************************************************************************/
|
---|
22 | #include <VBox/com/com.h>
|
---|
23 | #include <VBox/com/string.h>
|
---|
24 | #include <VBox/com/Guid.h>
|
---|
25 | #include <VBox/com/array.h>
|
---|
26 | #include <VBox/com/ErrorInfo.h>
|
---|
27 | #include <VBox/com/errorprint.h>
|
---|
28 |
|
---|
29 | #include <VBox/com/EventQueue.h>
|
---|
30 | #include <VBox/com/listeners.h>
|
---|
31 | #include <VBox/com/VirtualBox.h>
|
---|
32 |
|
---|
33 | #include <VBox/err.h>
|
---|
34 | #include <VBox/log.h>
|
---|
35 | #include <VBox/version.h>
|
---|
36 |
|
---|
37 | #include <package-generated.h>
|
---|
38 |
|
---|
39 | #include <iprt/asm.h>
|
---|
40 | #include <iprt/buildconfig.h>
|
---|
41 | #include <iprt/critsect.h>
|
---|
42 | #include <iprt/getopt.h>
|
---|
43 | #include <iprt/initterm.h>
|
---|
44 | #include <iprt/message.h>
|
---|
45 | #include <iprt/path.h>
|
---|
46 | #include <iprt/process.h>
|
---|
47 | #include <iprt/semaphore.h>
|
---|
48 | #include <iprt/stream.h>
|
---|
49 | #include <iprt/string.h>
|
---|
50 | #include <iprt/system.h>
|
---|
51 | #include <iprt/time.h>
|
---|
52 | #include <iprt/ctype.h>
|
---|
53 | #include <iprt/dir.h>
|
---|
54 |
|
---|
55 | #include <algorithm>
|
---|
56 | #include <list>
|
---|
57 | #include <string>
|
---|
58 | #include <signal.h>
|
---|
59 |
|
---|
60 | #include "VBoxAutostart.h"
|
---|
61 |
|
---|
62 | using namespace com;
|
---|
63 |
|
---|
64 | #if defined(RT_OS_LINUX) || defined (RT_OS_SOLARIS) || defined(RT_OS_FREEBSD) || defined(RT_OS_DARWIN)
|
---|
65 | # define VBOXAUTOSTART_DAEMONIZE
|
---|
66 | #endif
|
---|
67 |
|
---|
68 | ComPtr<IVirtualBoxClient> g_pVirtualBoxClient = NULL;
|
---|
69 | bool g_fVerbose = false;
|
---|
70 | ComPtr<IVirtualBox> g_pVirtualBox = NULL;
|
---|
71 | ComPtr<ISession> g_pSession = NULL;
|
---|
72 |
|
---|
73 | /** Logging parameters. */
|
---|
74 | static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
|
---|
75 | static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
|
---|
76 | static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
|
---|
77 |
|
---|
78 | /** Run in background. */
|
---|
79 | static bool g_fDaemonize = false;
|
---|
80 |
|
---|
81 | /**
|
---|
82 | * Command line arguments.
|
---|
83 | */
|
---|
84 | static const RTGETOPTDEF g_aOptions[] = {
|
---|
85 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
86 | { "--background", 'b', RTGETOPT_REQ_NOTHING },
|
---|
87 | #endif
|
---|
88 | /** For displayHelp(). */
|
---|
89 | { "--help", 'h', RTGETOPT_REQ_NOTHING },
|
---|
90 | { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
|
---|
91 | { "--start", 's', RTGETOPT_REQ_NOTHING },
|
---|
92 | { "--stop", 'd', RTGETOPT_REQ_NOTHING },
|
---|
93 | { "--config", 'c', RTGETOPT_REQ_STRING },
|
---|
94 | { "--logfile", 'F', RTGETOPT_REQ_STRING },
|
---|
95 | { "--logrotate", 'R', RTGETOPT_REQ_UINT32 },
|
---|
96 | { "--logsize", 'S', RTGETOPT_REQ_UINT64 },
|
---|
97 | { "--loginterval", 'I', RTGETOPT_REQ_UINT32 },
|
---|
98 | { "--quiet", 'Q', RTGETOPT_REQ_NOTHING }
|
---|
99 | };
|
---|
100 |
|
---|
101 |
|
---|
102 | DECLHIDDEN(void) serviceLog(const char *pszFormat, ...)
|
---|
103 | {
|
---|
104 | va_list args;
|
---|
105 | va_start(args, pszFormat);
|
---|
106 | char *psz = NULL;
|
---|
107 | RTStrAPrintfV(&psz, pszFormat, args);
|
---|
108 | va_end(args);
|
---|
109 |
|
---|
110 | LogRel(("%s", psz));
|
---|
111 |
|
---|
112 | RTStrFree(psz);
|
---|
113 | }
|
---|
114 |
|
---|
115 | static void displayHeader()
|
---|
116 | {
|
---|
117 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Autostart " VBOX_VERSION_STRING "\n"
|
---|
118 | "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
119 | "All rights reserved.\n\n");
|
---|
120 | }
|
---|
121 |
|
---|
122 | /**
|
---|
123 | * Displays the help.
|
---|
124 | *
|
---|
125 | * @param pszImage Name of program name (image).
|
---|
126 | */
|
---|
127 | static void displayHelp(const char *pszImage)
|
---|
128 | {
|
---|
129 | AssertPtrReturnVoid(pszImage);
|
---|
130 |
|
---|
131 | displayHeader();
|
---|
132 |
|
---|
133 | RTStrmPrintf(g_pStdErr,
|
---|
134 | "Usage:\n"
|
---|
135 | " %s [-v|--verbose] [-h|-?|--help]\n"
|
---|
136 | " [-F|--logfile=<file>] [-R|--logrotate=<num>] [-S|--logsize=<bytes>]\n"
|
---|
137 | " [-I|--loginterval=<seconds>]\n"
|
---|
138 | " [-c|--config=<config file>]\n", pszImage);
|
---|
139 |
|
---|
140 | RTStrmPrintf(g_pStdErr, "\n"
|
---|
141 | "Options:\n");
|
---|
142 |
|
---|
143 | for (unsigned i = 0;
|
---|
144 | i < RT_ELEMENTS(g_aOptions);
|
---|
145 | ++i)
|
---|
146 | {
|
---|
147 | std::string str(g_aOptions[i].pszLong);
|
---|
148 | if (g_aOptions[i].iShort < 1000) /* Don't show short options which are defined by an ID! */
|
---|
149 | {
|
---|
150 | str += ", -";
|
---|
151 | str += g_aOptions[i].iShort;
|
---|
152 | }
|
---|
153 | str += ":";
|
---|
154 |
|
---|
155 | const char *pcszDescr = "";
|
---|
156 |
|
---|
157 | switch (g_aOptions[i].iShort)
|
---|
158 | {
|
---|
159 | case 'h':
|
---|
160 | pcszDescr = "Print this help message and exit.";
|
---|
161 | break;
|
---|
162 |
|
---|
163 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
164 | case 'b':
|
---|
165 | pcszDescr = "Run in background (daemon mode).";
|
---|
166 | break;
|
---|
167 | #endif
|
---|
168 |
|
---|
169 | case 'F':
|
---|
170 | pcszDescr = "Name of file to write log to (no file).";
|
---|
171 | break;
|
---|
172 |
|
---|
173 | case 'R':
|
---|
174 | pcszDescr = "Number of log files (0 disables log rotation).";
|
---|
175 | break;
|
---|
176 |
|
---|
177 | case 'S':
|
---|
178 | pcszDescr = "Maximum size of a log file to trigger rotation (bytes).";
|
---|
179 | break;
|
---|
180 |
|
---|
181 | case 'I':
|
---|
182 | pcszDescr = "Maximum time interval to trigger log rotation (seconds).";
|
---|
183 | break;
|
---|
184 |
|
---|
185 | case 'c':
|
---|
186 | pcszDescr = "Name of the configuration file for the global overrides.";
|
---|
187 | break;
|
---|
188 | }
|
---|
189 |
|
---|
190 | RTStrmPrintf(g_pStdErr, "%-23s%s\n", str.c_str(), pcszDescr);
|
---|
191 | }
|
---|
192 |
|
---|
193 | RTStrmPrintf(g_pStdErr, "\nUse environment variable VBOXAUTOSTART_RELEASE_LOG for logging options.\n");
|
---|
194 | }
|
---|
195 |
|
---|
196 | /**
|
---|
197 | * Creates all global COM objects.
|
---|
198 | *
|
---|
199 | * @return HRESULT
|
---|
200 | */
|
---|
201 | static int autostartSetup()
|
---|
202 | {
|
---|
203 | serviceLogVerbose(("Setting up ...\n"));
|
---|
204 |
|
---|
205 | /*
|
---|
206 | * Setup VirtualBox + session interfaces.
|
---|
207 | */
|
---|
208 | HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
209 | if (SUCCEEDED(rc))
|
---|
210 | {
|
---|
211 | rc = g_pSession.createInprocObject(CLSID_Session);
|
---|
212 | if (FAILED(rc))
|
---|
213 | RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
|
---|
214 | }
|
---|
215 | else
|
---|
216 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
|
---|
217 |
|
---|
218 | if (FAILED(rc))
|
---|
219 | return VERR_COM_OBJECT_NOT_FOUND;
|
---|
220 |
|
---|
221 | return VINF_SUCCESS;
|
---|
222 | }
|
---|
223 |
|
---|
224 | static void autostartShutdown()
|
---|
225 | {
|
---|
226 | serviceLogVerbose(("Shutting down ...\n"));
|
---|
227 |
|
---|
228 | g_pSession.setNull();
|
---|
229 | g_pVirtualBox.setNull();
|
---|
230 | }
|
---|
231 |
|
---|
232 | int main(int argc, char *argv[])
|
---|
233 | {
|
---|
234 | /*
|
---|
235 | * Before we do anything, init the runtime without loading
|
---|
236 | * the support driver.
|
---|
237 | */
|
---|
238 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
239 | if (RT_FAILURE(rc))
|
---|
240 | return RTMsgInitFailure(rc);
|
---|
241 |
|
---|
242 | /*
|
---|
243 | * Parse the global options
|
---|
244 | */
|
---|
245 | int c;
|
---|
246 | const char *pszLogFile = NULL;
|
---|
247 | const char *pszConfigFile = NULL;
|
---|
248 | bool fQuiet = false;
|
---|
249 | bool fStart = false;
|
---|
250 | bool fStop = false;
|
---|
251 | RTGETOPTUNION ValueUnion;
|
---|
252 | RTGETOPTSTATE GetState;
|
---|
253 | RTGetOptInit(&GetState, argc, argv,
|
---|
254 | g_aOptions, RT_ELEMENTS(g_aOptions), 1 /* First */, 0 /*fFlags*/);
|
---|
255 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
256 | {
|
---|
257 | switch (c)
|
---|
258 | {
|
---|
259 | case 'h':
|
---|
260 | displayHelp(argv[0]);
|
---|
261 | return 0;
|
---|
262 |
|
---|
263 | case 'v':
|
---|
264 | g_fVerbose = true;
|
---|
265 | break;
|
---|
266 |
|
---|
267 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
268 | case 'b':
|
---|
269 | g_fDaemonize = true;
|
---|
270 | break;
|
---|
271 | #endif
|
---|
272 | case 'V':
|
---|
273 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
274 | return 0;
|
---|
275 |
|
---|
276 | case 'F':
|
---|
277 | pszLogFile = ValueUnion.psz;
|
---|
278 | break;
|
---|
279 |
|
---|
280 | case 'R':
|
---|
281 | g_cHistory = ValueUnion.u32;
|
---|
282 | break;
|
---|
283 |
|
---|
284 | case 'S':
|
---|
285 | g_uHistoryFileSize = ValueUnion.u64;
|
---|
286 | break;
|
---|
287 |
|
---|
288 | case 'I':
|
---|
289 | g_uHistoryFileTime = ValueUnion.u32;
|
---|
290 | break;
|
---|
291 |
|
---|
292 | case 'Q':
|
---|
293 | fQuiet = true;
|
---|
294 | break;
|
---|
295 |
|
---|
296 | case 'c':
|
---|
297 | pszConfigFile = ValueUnion.psz;
|
---|
298 | break;
|
---|
299 |
|
---|
300 | case 's':
|
---|
301 | fStart = true;
|
---|
302 | break;
|
---|
303 |
|
---|
304 | case 'd':
|
---|
305 | fStop = true;
|
---|
306 | break;
|
---|
307 |
|
---|
308 | default:
|
---|
309 | return RTGetOptPrintError(c, &ValueUnion);
|
---|
310 | }
|
---|
311 | }
|
---|
312 |
|
---|
313 | if (!fStart && !fStop)
|
---|
314 | {
|
---|
315 | displayHelp(argv[0]);
|
---|
316 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Either --start or --stop must be present");
|
---|
317 | }
|
---|
318 | else if (fStart && fStop)
|
---|
319 | {
|
---|
320 | displayHelp(argv[0]);
|
---|
321 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "--start or --stop are mutually exclusive");
|
---|
322 | }
|
---|
323 |
|
---|
324 | if (!pszConfigFile)
|
---|
325 | {
|
---|
326 | displayHelp(argv[0]);
|
---|
327 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "--config <config file> is missing");
|
---|
328 | }
|
---|
329 |
|
---|
330 | if (!fQuiet)
|
---|
331 | displayHeader();
|
---|
332 |
|
---|
333 | bool fAllowed = false;
|
---|
334 | uint32_t uStartupDelay = 0;
|
---|
335 | rc = autostartParseConfig(pszConfigFile, &fAllowed, &uStartupDelay);
|
---|
336 | if (RT_FAILURE(rc))
|
---|
337 | return RTEXITCODE_FAILURE;
|
---|
338 |
|
---|
339 | if (!fAllowed)
|
---|
340 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "User is not allowed to autostart VMs");
|
---|
341 |
|
---|
342 | /* Don't start if the VirtualBox settings directory does not exist. */
|
---|
343 | char szUserHomeDir[RTPATH_MAX];
|
---|
344 | rc = com::GetVBoxUserHomeDirectory(szUserHomeDir, sizeof(szUserHomeDir), false /* fCreateDir */);
|
---|
345 | if (RT_FAILURE(rc))
|
---|
346 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory: %Rrc", rc);
|
---|
347 | else if (!RTDirExists(szUserHomeDir))
|
---|
348 | return RTEXITCODE_SUCCESS;
|
---|
349 |
|
---|
350 | /* create release logger, to stdout */
|
---|
351 | char szError[RTPATH_MAX + 128];
|
---|
352 | rc = com::VBoxLogRelCreate("Autostart", g_fDaemonize ? NULL : pszLogFile,
|
---|
353 | RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
354 | "all", "VBOXAUTOSTART_RELEASE_LOG",
|
---|
355 | RTLOGDEST_STDOUT, UINT32_MAX /* cMaxEntriesPerGroup */,
|
---|
356 | g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
|
---|
357 | szError, sizeof(szError));
|
---|
358 | if (RT_FAILURE(rc))
|
---|
359 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, rc);
|
---|
360 |
|
---|
361 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
362 | if (g_fDaemonize)
|
---|
363 | {
|
---|
364 | /* prepare release logging */
|
---|
365 | char szLogFile[RTPATH_MAX];
|
---|
366 |
|
---|
367 | if (!pszLogFile || !*pszLogFile)
|
---|
368 | {
|
---|
369 | rc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
|
---|
370 | if (RT_FAILURE(rc))
|
---|
371 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory for logging: %Rrc", rc);
|
---|
372 | rc = RTPathAppend(szLogFile, sizeof(szLogFile), "vboxautostart.log");
|
---|
373 | if (RT_FAILURE(rc))
|
---|
374 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not construct logging path: %Rrc", rc);
|
---|
375 | pszLogFile = szLogFile;
|
---|
376 | }
|
---|
377 |
|
---|
378 | rc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, NULL);
|
---|
379 | if (RT_FAILURE(rc))
|
---|
380 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
|
---|
381 | /* create release logger, to file */
|
---|
382 | rc = com::VBoxLogRelCreate("Autostart", pszLogFile,
|
---|
383 | RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
384 | "all", "VBOXAUTOSTART_RELEASE_LOG",
|
---|
385 | RTLOGDEST_FILE, UINT32_MAX /* cMaxEntriesPerGroup */,
|
---|
386 | g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
|
---|
387 | szError, sizeof(szError));
|
---|
388 | if (RT_FAILURE(rc))
|
---|
389 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, rc);
|
---|
390 | }
|
---|
391 | #endif
|
---|
392 |
|
---|
393 | /*
|
---|
394 | * Initialize COM.
|
---|
395 | */
|
---|
396 | using namespace com;
|
---|
397 | HRESULT hrc = com::Initialize();
|
---|
398 | # ifdef VBOX_WITH_XPCOM
|
---|
399 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
400 | {
|
---|
401 | char szHome[RTPATH_MAX] = "";
|
---|
402 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
403 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
404 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
405 | }
|
---|
406 | # endif
|
---|
407 | if (FAILED(hrc))
|
---|
408 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
|
---|
409 |
|
---|
410 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
411 | if (FAILED(hrc))
|
---|
412 | {
|
---|
413 | RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
|
---|
414 | com::ErrorInfo info;
|
---|
415 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
416 | {
|
---|
417 | com::GluePrintRCMessage(hrc);
|
---|
418 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
419 | }
|
---|
420 | else
|
---|
421 | com::GluePrintErrorInfo(info);
|
---|
422 | return RTEXITCODE_FAILURE;
|
---|
423 | }
|
---|
424 |
|
---|
425 | rc = autostartSetup();
|
---|
426 | if (RT_FAILURE(rc))
|
---|
427 | return RTEXITCODE_FAILURE;
|
---|
428 |
|
---|
429 | RTEXITCODE rcExit;
|
---|
430 | if (fStart)
|
---|
431 | rcExit = autostartStartMain(uStartupDelay);
|
---|
432 | else
|
---|
433 | {
|
---|
434 | Assert(fStop);
|
---|
435 | rcExit = autostartStopMain(uStartupDelay);
|
---|
436 | }
|
---|
437 |
|
---|
438 | EventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
439 |
|
---|
440 | autostartShutdown();
|
---|
441 |
|
---|
442 | g_pVirtualBoxClient.setNull();
|
---|
443 |
|
---|
444 | com::Shutdown();
|
---|
445 |
|
---|
446 | return rcExit;
|
---|
447 | }
|
---|
448 |
|
---|