1 | /* $Id: VBoxAutostart-posix.cpp 43909 2012-11-19 10:00:02Z 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 | /** Set by the signal handler. */
|
---|
102 | static volatile bool g_fCanceled = false;
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Signal handler that sets g_fCanceled.
|
---|
107 | *
|
---|
108 | * This can be executed on any thread in the process, on Windows it may even be
|
---|
109 | * a thread dedicated to delivering this signal. Do not doing anything
|
---|
110 | * unnecessary here.
|
---|
111 | */
|
---|
112 | static void showProgressSignalHandler(int iSignal)
|
---|
113 | {
|
---|
114 | NOREF(iSignal);
|
---|
115 | ASMAtomicWriteBool(&g_fCanceled, true);
|
---|
116 | }
|
---|
117 |
|
---|
118 | /**
|
---|
119 | * Print out progress on the console.
|
---|
120 | *
|
---|
121 | * This runs the main event queue every now and then to prevent piling up
|
---|
122 | * unhandled things (which doesn't cause real problems, just makes things
|
---|
123 | * react a little slower than in the ideal case).
|
---|
124 | */
|
---|
125 | DECLHIDDEN(HRESULT) showProgress(ComPtr<IProgress> progress)
|
---|
126 | {
|
---|
127 | using namespace com;
|
---|
128 |
|
---|
129 | BOOL fCompleted = FALSE;
|
---|
130 | ULONG ulCurrentPercent = 0;
|
---|
131 | ULONG ulLastPercent = 0;
|
---|
132 |
|
---|
133 | ULONG ulLastOperationPercent = (ULONG)-1;
|
---|
134 |
|
---|
135 | ULONG ulLastOperation = (ULONG)-1;
|
---|
136 | Bstr bstrOperationDescription;
|
---|
137 |
|
---|
138 | EventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
139 |
|
---|
140 | ULONG cOperations = 1;
|
---|
141 | HRESULT hrc = progress->COMGETTER(OperationCount)(&cOperations);
|
---|
142 | if (FAILED(hrc))
|
---|
143 | {
|
---|
144 | RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
|
---|
145 | RTStrmFlush(g_pStdErr);
|
---|
146 | return hrc;
|
---|
147 | }
|
---|
148 |
|
---|
149 | /*
|
---|
150 | * Note: Outputting the progress info to stderr (g_pStdErr) is intentional
|
---|
151 | * to not get intermixed with other (raw) stdout data which might get
|
---|
152 | * written in the meanwhile.
|
---|
153 | */
|
---|
154 | RTStrmPrintf(g_pStdErr, "0%%...");
|
---|
155 | RTStrmFlush(g_pStdErr);
|
---|
156 |
|
---|
157 | /* setup signal handling if cancelable */
|
---|
158 | bool fCanceledAlready = false;
|
---|
159 | BOOL fCancelable;
|
---|
160 | hrc = progress->COMGETTER(Cancelable)(&fCancelable);
|
---|
161 | if (FAILED(hrc))
|
---|
162 | fCancelable = FALSE;
|
---|
163 | if (fCancelable)
|
---|
164 | {
|
---|
165 | signal(SIGINT, showProgressSignalHandler);
|
---|
166 | #ifdef SIGBREAK
|
---|
167 | signal(SIGBREAK, showProgressSignalHandler);
|
---|
168 | #endif
|
---|
169 | }
|
---|
170 |
|
---|
171 | hrc = progress->COMGETTER(Completed(&fCompleted));
|
---|
172 | while (SUCCEEDED(hrc))
|
---|
173 | {
|
---|
174 | progress->COMGETTER(Percent(&ulCurrentPercent));
|
---|
175 |
|
---|
176 | /* did we cross a 10% mark? */
|
---|
177 | if (ulCurrentPercent / 10 > ulLastPercent / 10)
|
---|
178 | {
|
---|
179 | /* make sure to also print out missed steps */
|
---|
180 | for (ULONG curVal = (ulLastPercent / 10) * 10 + 10; curVal <= (ulCurrentPercent / 10) * 10; curVal += 10)
|
---|
181 | {
|
---|
182 | if (curVal < 100)
|
---|
183 | {
|
---|
184 | RTStrmPrintf(g_pStdErr, "%u%%...", curVal);
|
---|
185 | RTStrmFlush(g_pStdErr);
|
---|
186 | }
|
---|
187 | }
|
---|
188 | ulLastPercent = (ulCurrentPercent / 10) * 10;
|
---|
189 | }
|
---|
190 |
|
---|
191 | if (fCompleted)
|
---|
192 | break;
|
---|
193 |
|
---|
194 | /* process async cancelation */
|
---|
195 | if (g_fCanceled && !fCanceledAlready)
|
---|
196 | {
|
---|
197 | hrc = progress->Cancel();
|
---|
198 | if (SUCCEEDED(hrc))
|
---|
199 | fCanceledAlready = true;
|
---|
200 | else
|
---|
201 | g_fCanceled = false;
|
---|
202 | }
|
---|
203 |
|
---|
204 | /* make sure the loop is not too tight */
|
---|
205 | progress->WaitForCompletion(100);
|
---|
206 |
|
---|
207 | EventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
208 | hrc = progress->COMGETTER(Completed(&fCompleted));
|
---|
209 | }
|
---|
210 |
|
---|
211 | /* undo signal handling */
|
---|
212 | if (fCancelable)
|
---|
213 | {
|
---|
214 | signal(SIGINT, SIG_DFL);
|
---|
215 | #ifdef SIGBREAK
|
---|
216 | signal(SIGBREAK, SIG_DFL);
|
---|
217 | #endif
|
---|
218 | }
|
---|
219 |
|
---|
220 | /* complete the line. */
|
---|
221 | LONG iRc = E_FAIL;
|
---|
222 | hrc = progress->COMGETTER(ResultCode)(&iRc);
|
---|
223 | if (SUCCEEDED(hrc))
|
---|
224 | {
|
---|
225 | if (SUCCEEDED(iRc))
|
---|
226 | RTStrmPrintf(g_pStdErr, "100%%\n");
|
---|
227 | else if (g_fCanceled)
|
---|
228 | RTStrmPrintf(g_pStdErr, "CANCELED\n");
|
---|
229 | else
|
---|
230 | {
|
---|
231 | RTStrmPrintf(g_pStdErr, "\n");
|
---|
232 | RTStrmPrintf(g_pStdErr, "Progress state: %Rhrc\n", iRc);
|
---|
233 | }
|
---|
234 | hrc = iRc;
|
---|
235 | }
|
---|
236 | else
|
---|
237 | {
|
---|
238 | RTStrmPrintf(g_pStdErr, "\n");
|
---|
239 | RTStrmPrintf(g_pStdErr, "Progress object failure: %Rhrc\n", hrc);
|
---|
240 | }
|
---|
241 | RTStrmFlush(g_pStdErr);
|
---|
242 | return hrc;
|
---|
243 | }
|
---|
244 |
|
---|
245 | DECLHIDDEN(void) serviceLog(const char *pszFormat, ...)
|
---|
246 | {
|
---|
247 | va_list args;
|
---|
248 | va_start(args, pszFormat);
|
---|
249 | char *psz = NULL;
|
---|
250 | RTStrAPrintfV(&psz, pszFormat, args);
|
---|
251 | va_end(args);
|
---|
252 |
|
---|
253 | LogRel(("%s", psz));
|
---|
254 |
|
---|
255 | RTStrFree(psz);
|
---|
256 | }
|
---|
257 |
|
---|
258 | static void displayHeader()
|
---|
259 | {
|
---|
260 | RTStrmPrintf(g_pStdErr, VBOX_PRODUCT " Autostart " VBOX_VERSION_STRING "\n"
|
---|
261 | "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
|
---|
262 | "All rights reserved.\n\n");
|
---|
263 | }
|
---|
264 |
|
---|
265 | /**
|
---|
266 | * Displays the help.
|
---|
267 | *
|
---|
268 | * @param pszImage Name of program name (image).
|
---|
269 | */
|
---|
270 | static void displayHelp(const char *pszImage)
|
---|
271 | {
|
---|
272 | AssertPtrReturnVoid(pszImage);
|
---|
273 |
|
---|
274 | displayHeader();
|
---|
275 |
|
---|
276 | RTStrmPrintf(g_pStdErr,
|
---|
277 | "Usage:\n"
|
---|
278 | " %s [-v|--verbose] [-h|-?|--help]\n"
|
---|
279 | " [-F|--logfile=<file>] [-R|--logrotate=<num>] [-S|--logsize=<bytes>]\n"
|
---|
280 | " [-I|--loginterval=<seconds>]\n"
|
---|
281 | " [-c|--config=<config file>]\n", pszImage);
|
---|
282 |
|
---|
283 | RTStrmPrintf(g_pStdErr, "\n"
|
---|
284 | "Options:\n");
|
---|
285 |
|
---|
286 | for (unsigned i = 0;
|
---|
287 | i < RT_ELEMENTS(g_aOptions);
|
---|
288 | ++i)
|
---|
289 | {
|
---|
290 | std::string str(g_aOptions[i].pszLong);
|
---|
291 | if (g_aOptions[i].iShort < 1000) /* Don't show short options which are defined by an ID! */
|
---|
292 | {
|
---|
293 | str += ", -";
|
---|
294 | str += g_aOptions[i].iShort;
|
---|
295 | }
|
---|
296 | str += ":";
|
---|
297 |
|
---|
298 | const char *pcszDescr = "";
|
---|
299 |
|
---|
300 | switch (g_aOptions[i].iShort)
|
---|
301 | {
|
---|
302 | case 'h':
|
---|
303 | pcszDescr = "Print this help message and exit.";
|
---|
304 | break;
|
---|
305 |
|
---|
306 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
307 | case 'b':
|
---|
308 | pcszDescr = "Run in background (daemon mode).";
|
---|
309 | break;
|
---|
310 | #endif
|
---|
311 |
|
---|
312 | case 'F':
|
---|
313 | pcszDescr = "Name of file to write log to (no file).";
|
---|
314 | break;
|
---|
315 |
|
---|
316 | case 'R':
|
---|
317 | pcszDescr = "Number of log files (0 disables log rotation).";
|
---|
318 | break;
|
---|
319 |
|
---|
320 | case 'S':
|
---|
321 | pcszDescr = "Maximum size of a log file to trigger rotation (bytes).";
|
---|
322 | break;
|
---|
323 |
|
---|
324 | case 'I':
|
---|
325 | pcszDescr = "Maximum time interval to trigger log rotation (seconds).";
|
---|
326 | break;
|
---|
327 |
|
---|
328 | case 'c':
|
---|
329 | pcszDescr = "Name of the configuration file for the global overrides.";
|
---|
330 | break;
|
---|
331 | }
|
---|
332 |
|
---|
333 | RTStrmPrintf(g_pStdErr, "%-23s%s\n", str.c_str(), pcszDescr);
|
---|
334 | }
|
---|
335 |
|
---|
336 | RTStrmPrintf(g_pStdErr, "\nUse environment variable VBOXAUTOSTART_RELEASE_LOG for logging options.\n");
|
---|
337 | }
|
---|
338 |
|
---|
339 | /**
|
---|
340 | * Creates all global COM objects.
|
---|
341 | *
|
---|
342 | * @return HRESULT
|
---|
343 | */
|
---|
344 | static int autostartSetup()
|
---|
345 | {
|
---|
346 | serviceLogVerbose(("Setting up ...\n"));
|
---|
347 |
|
---|
348 | /*
|
---|
349 | * Setup VirtualBox + session interfaces.
|
---|
350 | */
|
---|
351 | HRESULT rc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
|
---|
352 | if (SUCCEEDED(rc))
|
---|
353 | {
|
---|
354 | rc = g_pSession.createInprocObject(CLSID_Session);
|
---|
355 | if (FAILED(rc))
|
---|
356 | RTMsgError("Failed to create a session object (rc=%Rhrc)!", rc);
|
---|
357 | }
|
---|
358 | else
|
---|
359 | RTMsgError("Failed to get VirtualBox object (rc=%Rhrc)!", rc);
|
---|
360 |
|
---|
361 | if (FAILED(rc))
|
---|
362 | return VERR_COM_OBJECT_NOT_FOUND;
|
---|
363 |
|
---|
364 | return VINF_SUCCESS;
|
---|
365 | }
|
---|
366 |
|
---|
367 | static void autostartShutdown()
|
---|
368 | {
|
---|
369 | serviceLogVerbose(("Shutting down ...\n"));
|
---|
370 |
|
---|
371 | g_pSession.setNull();
|
---|
372 | g_pVirtualBox.setNull();
|
---|
373 | }
|
---|
374 |
|
---|
375 | int main(int argc, char *argv[])
|
---|
376 | {
|
---|
377 | /*
|
---|
378 | * Before we do anything, init the runtime without loading
|
---|
379 | * the support driver.
|
---|
380 | */
|
---|
381 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
382 | if (RT_FAILURE(rc))
|
---|
383 | return RTMsgInitFailure(rc);
|
---|
384 |
|
---|
385 | /*
|
---|
386 | * Parse the global options
|
---|
387 | */
|
---|
388 | int c;
|
---|
389 | const char *pszLogFile = NULL;
|
---|
390 | const char *pszConfigFile = NULL;
|
---|
391 | bool fQuiet = false;
|
---|
392 | bool fStart = false;
|
---|
393 | bool fStop = false;
|
---|
394 | RTGETOPTUNION ValueUnion;
|
---|
395 | RTGETOPTSTATE GetState;
|
---|
396 | RTGetOptInit(&GetState, argc, argv,
|
---|
397 | g_aOptions, RT_ELEMENTS(g_aOptions), 1 /* First */, 0 /*fFlags*/);
|
---|
398 | while ((c = RTGetOpt(&GetState, &ValueUnion)))
|
---|
399 | {
|
---|
400 | switch (c)
|
---|
401 | {
|
---|
402 | case 'h':
|
---|
403 | displayHelp(argv[0]);
|
---|
404 | return 0;
|
---|
405 |
|
---|
406 | case 'v':
|
---|
407 | g_fVerbose = true;
|
---|
408 | break;
|
---|
409 |
|
---|
410 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
411 | case 'b':
|
---|
412 | g_fDaemonize = true;
|
---|
413 | break;
|
---|
414 | #endif
|
---|
415 | case 'V':
|
---|
416 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
417 | return 0;
|
---|
418 |
|
---|
419 | case 'F':
|
---|
420 | pszLogFile = ValueUnion.psz;
|
---|
421 | break;
|
---|
422 |
|
---|
423 | case 'R':
|
---|
424 | g_cHistory = ValueUnion.u32;
|
---|
425 | break;
|
---|
426 |
|
---|
427 | case 'S':
|
---|
428 | g_uHistoryFileSize = ValueUnion.u64;
|
---|
429 | break;
|
---|
430 |
|
---|
431 | case 'I':
|
---|
432 | g_uHistoryFileTime = ValueUnion.u32;
|
---|
433 | break;
|
---|
434 |
|
---|
435 | case 'Q':
|
---|
436 | fQuiet = true;
|
---|
437 | break;
|
---|
438 |
|
---|
439 | case 'c':
|
---|
440 | pszConfigFile = ValueUnion.psz;
|
---|
441 | break;
|
---|
442 |
|
---|
443 | case 's':
|
---|
444 | fStart = true;
|
---|
445 | break;
|
---|
446 |
|
---|
447 | case 'd':
|
---|
448 | fStop = true;
|
---|
449 | break;
|
---|
450 |
|
---|
451 | default:
|
---|
452 | return RTGetOptPrintError(c, &ValueUnion);
|
---|
453 | }
|
---|
454 | }
|
---|
455 |
|
---|
456 | if (!fStart && !fStop)
|
---|
457 | {
|
---|
458 | displayHelp(argv[0]);
|
---|
459 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Either --start or --stop must be present");
|
---|
460 | }
|
---|
461 | else if (fStart && fStop)
|
---|
462 | {
|
---|
463 | displayHelp(argv[0]);
|
---|
464 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "--start or --stop are mutually exclusive");
|
---|
465 | }
|
---|
466 |
|
---|
467 | if (!pszConfigFile)
|
---|
468 | {
|
---|
469 | displayHelp(argv[0]);
|
---|
470 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "--config <config file> is missing");
|
---|
471 | }
|
---|
472 |
|
---|
473 | if (!fQuiet)
|
---|
474 | displayHeader();
|
---|
475 |
|
---|
476 | PCFGAST pCfgAst = NULL;
|
---|
477 | char *pszUser = NULL;
|
---|
478 | PCFGAST pCfgAstUser = NULL;
|
---|
479 | PCFGAST pCfgAstPolicy = NULL;
|
---|
480 | bool fAllow = false;
|
---|
481 |
|
---|
482 | rc = autostartParseConfig(pszConfigFile, &pCfgAst);
|
---|
483 | if (RT_FAILURE(rc))
|
---|
484 | return RTEXITCODE_FAILURE;
|
---|
485 |
|
---|
486 | rc = RTProcQueryUsernameA(RTProcSelf(), &pszUser);
|
---|
487 | if (RT_FAILURE(rc))
|
---|
488 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to query username of the process");
|
---|
489 |
|
---|
490 | pCfgAstUser = autostartConfigAstGetByName(pCfgAst, pszUser);
|
---|
491 | pCfgAstPolicy = autostartConfigAstGetByName(pCfgAst, "default_policy");
|
---|
492 |
|
---|
493 | /* Check default policy. */
|
---|
494 | if (pCfgAstPolicy)
|
---|
495 | {
|
---|
496 | if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
|
---|
497 | && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow")
|
---|
498 | || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "deny")))
|
---|
499 | {
|
---|
500 | if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "allow"))
|
---|
501 | fAllow = true;
|
---|
502 | }
|
---|
503 | else
|
---|
504 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "'default_policy' must be either 'allow' or 'deny'");
|
---|
505 | }
|
---|
506 |
|
---|
507 | if ( pCfgAstUser
|
---|
508 | && pCfgAstUser->enmType == CFGASTNODETYPE_COMPOUND)
|
---|
509 | {
|
---|
510 | pCfgAstPolicy = autostartConfigAstGetByName(pCfgAstUser, "allow");
|
---|
511 | if (pCfgAstPolicy)
|
---|
512 | {
|
---|
513 | if ( pCfgAstPolicy->enmType == CFGASTNODETYPE_KEYVALUE
|
---|
514 | && ( !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true")
|
---|
515 | || !RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "false")))
|
---|
516 | {
|
---|
517 | if (!RTStrCmp(pCfgAstPolicy->u.KeyValue.aszValue, "true"))
|
---|
518 | fAllow = true;
|
---|
519 | else
|
---|
520 | fAllow = false;
|
---|
521 | }
|
---|
522 | else
|
---|
523 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "'allow' must be either 'true' or 'false'");
|
---|
524 | }
|
---|
525 | }
|
---|
526 | else if (pCfgAstUser)
|
---|
527 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Invalid config, user is not a compound node");
|
---|
528 |
|
---|
529 | if (!fAllow)
|
---|
530 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "User is not allowed to autostart VMs");
|
---|
531 |
|
---|
532 | RTStrFree(pszUser);
|
---|
533 |
|
---|
534 | /* Don't start if the VirtualBox settings directory does not exist. */
|
---|
535 | char szUserHomeDir[RTPATH_MAX];
|
---|
536 | rc = com::GetVBoxUserHomeDirectory(szUserHomeDir, sizeof(szUserHomeDir), false /* fCreateDir */);
|
---|
537 | if (RT_FAILURE(rc))
|
---|
538 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory: %Rrc", rc);
|
---|
539 | else if (!RTDirExists(szUserHomeDir))
|
---|
540 | return RTEXITCODE_SUCCESS;
|
---|
541 |
|
---|
542 | /* create release logger, to stdout */
|
---|
543 | char szError[RTPATH_MAX + 128];
|
---|
544 | rc = com::VBoxLogRelCreate("Autostart", g_fDaemonize ? NULL : pszLogFile,
|
---|
545 | RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
546 | "all", "VBOXAUTOSTART_RELEASE_LOG",
|
---|
547 | RTLOGDEST_STDOUT, UINT32_MAX /* cMaxEntriesPerGroup */,
|
---|
548 | g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
|
---|
549 | szError, sizeof(szError));
|
---|
550 | if (RT_FAILURE(rc))
|
---|
551 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, rc);
|
---|
552 |
|
---|
553 | #ifdef VBOXAUTOSTART_DAEMONIZE
|
---|
554 | if (g_fDaemonize)
|
---|
555 | {
|
---|
556 | /* prepare release logging */
|
---|
557 | char szLogFile[RTPATH_MAX];
|
---|
558 |
|
---|
559 | if (!pszLogFile || !*pszLogFile)
|
---|
560 | {
|
---|
561 | rc = com::GetVBoxUserHomeDirectory(szLogFile, sizeof(szLogFile));
|
---|
562 | if (RT_FAILURE(rc))
|
---|
563 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not get base directory for logging: %Rrc", rc);
|
---|
564 | rc = RTPathAppend(szLogFile, sizeof(szLogFile), "vboxautostart.log");
|
---|
565 | if (RT_FAILURE(rc))
|
---|
566 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "could not construct logging path: %Rrc", rc);
|
---|
567 | pszLogFile = szLogFile;
|
---|
568 | }
|
---|
569 |
|
---|
570 | rc = RTProcDaemonizeUsingFork(false /* fNoChDir */, false /* fNoClose */, NULL);
|
---|
571 | if (RT_FAILURE(rc))
|
---|
572 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to daemonize, rc=%Rrc. exiting.", rc);
|
---|
573 | /* create release logger, to file */
|
---|
574 | rc = com::VBoxLogRelCreate("Autostart", pszLogFile,
|
---|
575 | RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME_PROG,
|
---|
576 | "all", "VBOXAUTOSTART_RELEASE_LOG",
|
---|
577 | RTLOGDEST_FILE, UINT32_MAX /* cMaxEntriesPerGroup */,
|
---|
578 | g_cHistory, g_uHistoryFileTime, g_uHistoryFileSize,
|
---|
579 | szError, sizeof(szError));
|
---|
580 | if (RT_FAILURE(rc))
|
---|
581 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "failed to open release log (%s, %Rrc)", szError, rc);
|
---|
582 | }
|
---|
583 | #endif
|
---|
584 |
|
---|
585 | /*
|
---|
586 | * Initialize COM.
|
---|
587 | */
|
---|
588 | using namespace com;
|
---|
589 | HRESULT hrc = com::Initialize();
|
---|
590 | # ifdef VBOX_WITH_XPCOM
|
---|
591 | if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
|
---|
592 | {
|
---|
593 | char szHome[RTPATH_MAX] = "";
|
---|
594 | com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
|
---|
595 | return RTMsgErrorExit(RTEXITCODE_FAILURE,
|
---|
596 | "Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
|
---|
597 | }
|
---|
598 | # endif
|
---|
599 | if (FAILED(hrc))
|
---|
600 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
|
---|
601 |
|
---|
602 | hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
|
---|
603 | if (FAILED(hrc))
|
---|
604 | {
|
---|
605 | RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
|
---|
606 | com::ErrorInfo info;
|
---|
607 | if (!info.isFullAvailable() && !info.isBasicAvailable())
|
---|
608 | {
|
---|
609 | com::GluePrintRCMessage(hrc);
|
---|
610 | RTMsgError("Most likely, the VirtualBox COM server is not running or failed to start.");
|
---|
611 | }
|
---|
612 | else
|
---|
613 | com::GluePrintErrorInfo(info);
|
---|
614 | return RTEXITCODE_FAILURE;
|
---|
615 | }
|
---|
616 |
|
---|
617 | rc = autostartSetup();
|
---|
618 | if (RT_FAILURE(rc))
|
---|
619 | return RTEXITCODE_FAILURE;
|
---|
620 |
|
---|
621 | RTEXITCODE rcExit;
|
---|
622 | if (fStart)
|
---|
623 | rcExit = autostartStartMain(pCfgAstUser);
|
---|
624 | else
|
---|
625 | {
|
---|
626 | Assert(fStop);
|
---|
627 | rcExit = autostartStopMain(pCfgAstUser);
|
---|
628 | }
|
---|
629 |
|
---|
630 | autostartConfigAstDestroy(pCfgAst);
|
---|
631 | EventQueue::getMainEventQueue()->processEventQueue(0);
|
---|
632 |
|
---|
633 | autostartShutdown();
|
---|
634 |
|
---|
635 | g_pVirtualBoxClient.setNull();
|
---|
636 |
|
---|
637 | com::Shutdown();
|
---|
638 |
|
---|
639 | return rcExit;
|
---|
640 | }
|
---|
641 |
|
---|