VirtualBox

source: vbox/trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp@ 35060

Last change on this file since 35060 was 35027, checked in by vboxsync, 14 years ago

Logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.5 KB
Line 
1/* $Id: VBoxService.cpp 35027 2010-12-13 16:07:56Z vboxsync $ */
2/** @file
3 * VBoxService - Guest Additions Service Skeleton.
4 */
5
6/*
7 * Copyright (C) 2007-2010 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/*******************************************************************************
21* Header Files *
22*******************************************************************************/
23/** @todo LOG_GROUP*/
24#ifndef _MSC_VER
25# include <unistd.h>
26#endif
27#include <errno.h>
28#ifndef RT_OS_WINDOWS
29# include <signal.h>
30# ifdef RT_OS_OS2
31# define pthread_sigmask sigprocmask
32# endif
33#endif
34#ifdef RT_OS_FREEBSD
35# include <pthread.h>
36#endif
37
38#include "product-generated.h"
39#include <iprt/asm.h>
40#include <iprt/buildconfig.h>
41#include <iprt/initterm.h>
42#include <iprt/path.h>
43#include <iprt/semaphore.h>
44#include <iprt/string.h>
45#include <iprt/stream.h>
46#include <iprt/thread.h>
47
48#include <VBox/VBoxGuestLib.h>
49#include <VBox/log.h>
50
51#include "VBoxServiceInternal.h"
52
53
54/*******************************************************************************
55* Global Variables *
56*******************************************************************************/
57/** The program name (derived from argv[0]). */
58char *g_pszProgName = (char *)"";
59/** The current verbosity level. */
60int g_cVerbosity = 0;
61/** The default service interval (the -i | --interval) option). */
62uint32_t g_DefaultInterval = 0;
63#ifdef RT_OS_WINDOWS
64/** Signal shutdown to the Windows service thread. */
65static bool volatile g_fWindowsServiceShutdown;
66/** Event the Windows service thread waits for shutdown. */
67static RTSEMEVENT g_hEvtWindowsService;
68#endif
69
70/**
71 * The details of the services that has been compiled in.
72 */
73static struct
74{
75 /** Pointer to the service descriptor. */
76 PCVBOXSERVICE pDesc;
77 /** The worker thread. NIL_RTTHREAD if it's the main thread. */
78 RTTHREAD Thread;
79 /** Shutdown indicator. */
80 bool volatile fShutdown;
81 /** Indicator set by the service thread exiting. */
82 bool volatile fStopped;
83 /** Whether the service was started or not. */
84 bool fStarted;
85 /** Whether the service is enabled or not. */
86 bool fEnabled;
87} g_aServices[] =
88{
89#ifdef VBOXSERVICE_CONTROL
90 { &g_Control, NIL_RTTHREAD, false, false, false, true },
91#endif
92#ifdef VBOXSERVICE_TIMESYNC
93 { &g_TimeSync, NIL_RTTHREAD, false, false, false, true },
94#endif
95#ifdef VBOXSERVICE_CLIPBOARD
96 { &g_Clipboard, NIL_RTTHREAD, false, false, false, true },
97#endif
98#ifdef VBOXSERVICE_VMINFO
99 { &g_VMInfo, NIL_RTTHREAD, false, false, false, true },
100#endif
101#ifdef VBOXSERVICE_CPUHOTPLUG
102 { &g_CpuHotPlug, NIL_RTTHREAD, false, false, false, true },
103#endif
104#ifdef VBOXSERVICE_MANAGEMENT
105# ifdef VBOX_WITH_MEMBALLOON
106 { &g_MemBalloon, NIL_RTTHREAD, false, false, false, true },
107# endif
108 { &g_VMStatistics, NIL_RTTHREAD, false, false, false, true },
109#endif
110#if defined(VBOX_WITH_PAGE_SHARING) && defined(RT_OS_WINDOWS)
111 { &g_PageSharing, NIL_RTTHREAD, false, false, false, true },
112#endif
113#ifdef VBOX_WITH_SHARED_FOLDERS
114 { &g_AutoMount, NIL_RTTHREAD, false, false, false, true },
115#endif
116};
117
118
119/**
120 * Displays the program usage message.
121 *
122 * @returns 1.
123 */
124static int VBoxServiceUsage(void)
125{
126 RTPrintf("Usage:\n"
127 " %-12s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
128 " [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
129#ifdef RT_OS_WINDOWS
130 RTPrintf(" [-r|--register] [-u|--unregister]\n");
131#endif
132 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
133 if (g_aServices[j].pDesc->pszUsage)
134 RTPrintf("%s\n", g_aServices[j].pDesc->pszUsage);
135 RTPrintf("\n"
136 "Options:\n"
137 " -i | --interval The default interval.\n"
138 " -f | --foreground Don't daemonize the program. For debugging.\n"
139 " -v | --verbose Increment the verbosity level. For debugging.\n"
140 " -h | -? | --help Show this message and exit with status 1.\n"
141 );
142#ifdef RT_OS_WINDOWS
143 RTPrintf(" -r | --register Installs the service.\n"
144 " -u | --unregister Uninstall service.\n");
145#endif
146
147 RTPrintf("\n"
148 "Service-specific options:\n");
149 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
150 {
151 RTPrintf(" --enable-%-14s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
152 RTPrintf(" --disable-%-13s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
153 if (g_aServices[j].pDesc->pszOptions)
154 RTPrintf("%s", g_aServices[j].pDesc->pszOptions);
155 }
156 RTPrintf("\n"
157 " Copyright (C) 2009-" VBOX_C_YEAR " " VBOX_VENDOR "\n");
158
159 return 1;
160}
161
162
163/**
164 * Displays a syntax error message.
165 *
166 * @returns RTEXITCODE_SYNTAX.
167 * @param pszFormat The message text.
168 * @param ... Format arguments.
169 */
170RTEXITCODE VBoxServiceSyntax(const char *pszFormat, ...)
171{
172 RTStrmPrintf(g_pStdErr, "%s: syntax error: ", g_pszProgName);
173
174 va_list va;
175 va_start(va, pszFormat);
176 RTStrmPrintfV(g_pStdErr, pszFormat, va);
177 va_end(va);
178
179 return RTEXITCODE_SYNTAX;
180}
181
182
183/**
184 * Displays an error message.
185 *
186 * @returns RTEXITCODE_FAILURE.
187 * @param pszFormat The message text.
188 * @param ... Format arguments.
189 */
190RTEXITCODE VBoxServiceError(const char *pszFormat, ...)
191{
192 RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
193
194 va_list va;
195 va_start(va, pszFormat);
196 RTStrmPrintfV(g_pStdErr, pszFormat, va);
197 va_end(va);
198
199 va_start(va, pszFormat);
200 LogRel(("%s: Error: %N", g_pszProgName, pszFormat, &va));
201 va_end(va);
202
203 return RTEXITCODE_FAILURE;
204}
205
206
207/**
208 * Displays a verbose message.
209 *
210 * @returns 1
211 * @param pszFormat The message text.
212 * @param ... Format arguments.
213 */
214void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...)
215{
216 if (iLevel <= g_cVerbosity)
217 {
218 RTStrmPrintf(g_pStdOut, "%s: ", g_pszProgName);
219 va_list va;
220 va_start(va, pszFormat);
221 RTStrmPrintfV(g_pStdOut, pszFormat, va);
222 va_end(va);
223 va_start(va, pszFormat);
224 LogRel(("%s: %N", g_pszProgName, pszFormat, &va));
225 va_end(va);
226 }
227}
228
229
230/**
231 * Gets a 32-bit value argument.
232 *
233 * @returns 0 on success, non-zero exit code on error.
234 * @param argc The argument count.
235 * @param argv The argument vector
236 * @param psz Where in *pi to start looking for the value argument.
237 * @param pi Where to find and perhaps update the argument index.
238 * @param pu32 Where to store the 32-bit value.
239 * @param u32Min The minimum value.
240 * @param u32Max The maximum value.
241 */
242int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
243{
244 if (*psz == ':' || *psz == '=')
245 psz++;
246 if (!*psz)
247 {
248 if (*pi + 1 >= argc)
249 return VBoxServiceSyntax("Missing value for the '%s' argument\n", argv[*pi]);
250 psz = argv[++*pi];
251 }
252
253 char *pszNext;
254 int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
255 if (RT_FAILURE(rc) || *pszNext)
256 return VBoxServiceSyntax("Failed to convert interval '%s' to a number.\n", psz);
257 if (*pu32 < u32Min || *pu32 > u32Max)
258 return VBoxServiceSyntax("The timesync interval of %RU32 seconds is out of range [%RU32..%RU32].\n",
259 *pu32, u32Min, u32Max);
260 return 0;
261}
262
263
264/**
265 * The service thread.
266 *
267 * @returns Whatever the worker function returns.
268 * @param ThreadSelf My thread handle.
269 * @param pvUser The service index.
270 */
271static DECLCALLBACK(int) VBoxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
272{
273 const unsigned i = (uintptr_t)pvUser;
274
275#ifndef RT_OS_WINDOWS
276 /*
277 * Block all signals for this thread. Only the main thread will handle signals.
278 */
279 sigset_t signalMask;
280 sigfillset(&signalMask);
281 pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
282#endif
283
284 int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
285 ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
286 RTThreadUserSignal(ThreadSelf);
287 return rc;
288}
289
290
291/**
292 * Check if at least one service should be started.
293 */
294static bool VBoxServiceCheckStartedServices(void)
295{
296 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
297 if (g_aServices[j].fEnabled)
298 return true;
299
300 return false;
301}
302
303
304/**
305 * Starts the service.
306 *
307 * @returns VBox status code, errors are fully bitched.
308 */
309int VBoxServiceStartServices(void)
310{
311 int rc;
312
313 /*
314 * Initialize the services.
315 */
316 VBoxServiceVerbose(2, "Initializing services ...\n");
317 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
318 if (g_aServices[j].fEnabled)
319 {
320 rc = g_aServices[j].pDesc->pfnInit();
321 if (RT_FAILURE(rc))
322 {
323 if (rc != VERR_SERVICE_DISABLED)
324 {
325 VBoxServiceError("Service '%s' failed to initialize: %Rrc\n",
326 g_aServices[j].pDesc->pszName, rc);
327 return rc;
328 }
329 g_aServices[j].fEnabled = false;
330 VBoxServiceVerbose(0, "Service '%s' was disabled because of missing functionality\n",
331 g_aServices[j].pDesc->pszName);
332
333 }
334 }
335
336 /*
337 * Start the service(s).
338 */
339 VBoxServiceVerbose(2, "Starting services ...\n");
340 rc = VINF_SUCCESS;
341 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
342 {
343 if (!g_aServices[j].fEnabled)
344 continue;
345
346 VBoxServiceVerbose(2, "Starting service '%s' ...\n", g_aServices[j].pDesc->pszName);
347 rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
348 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
349 if (RT_FAILURE(rc))
350 {
351 VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
352 break;
353 }
354 g_aServices[j].fStarted = true;
355
356 /* wait for the thread to initialize */
357 RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
358 if (g_aServices[j].fShutdown)
359 {
360 VBoxServiceError("Service '%s' failed to start!\n", g_aServices[j].pDesc->pszName);
361 rc = VERR_GENERAL_FAILURE;
362 }
363 }
364
365 if (RT_SUCCESS(rc))
366 VBoxServiceVerbose(1, "All services started.\n");
367 else
368 VBoxServiceError("An error occcurred while the services!\n");
369 return rc;
370}
371
372
373/**
374 * Stops and terminates the services.
375 *
376 * This should be called even when VBoxServiceStartServices fails so it can
377 * clean up anything that we succeeded in starting.
378 */
379int VBoxServiceStopServices(void)
380{
381 int rc = VINF_SUCCESS;
382
383 /*
384 * Signal all the services.
385 */
386 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
387 ASMAtomicWriteBool(&g_aServices[j].fShutdown, true);
388
389 /*
390 * Do the pfnStop callback on all running services.
391 */
392 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
393 if (g_aServices[j].fStarted)
394 {
395 VBoxServiceVerbose(3, "Calling stop function for service '%s' ...\n", g_aServices[j].pDesc->pszName);
396 g_aServices[j].pDesc->pfnStop();
397 }
398
399 /*
400 * Wait for all the service threads to complete.
401 */
402 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
403 {
404 if (!g_aServices[j].fEnabled) /* Only stop services which were started before. */
405 continue;
406 if (g_aServices[j].Thread != NIL_RTTHREAD)
407 {
408 VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
409 for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
410 {
411 rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
412 if (RT_SUCCESS(rc))
413 break;
414#ifdef RT_OS_WINDOWS
415 /* Notify SCM that it takes a bit longer ... */
416 VBoxServiceWinSetStopPendingStatus(i + j*32);
417#endif
418 }
419 if (RT_FAILURE(rc))
420 VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
421 }
422 VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
423 g_aServices[j].pDesc->pfnTerm();
424 }
425
426#ifdef RT_OS_WINDOWS
427 /*
428 * Wake up and tell the main() thread that we're shutting down (it's
429 * sleeping in VBoxServiceMainWait).
430 */
431 if (g_hEvtWindowsService != NIL_RTSEMEVENT)
432 {
433 VBoxServiceVerbose(3, "Stopping the main thread...\n");
434 ASMAtomicWriteBool(&g_fWindowsServiceShutdown, true);
435 rc = RTSemEventSignal(g_hEvtWindowsService);
436 AssertRC(rc);
437 }
438#endif
439
440 VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc);
441 return rc;
442}
443
444
445/**
446 * Block the main thread until the service shuts down.
447 */
448void VBoxServiceMainWait(void)
449{
450 int rc;
451
452 /* Report the host that we're up and running! */
453 rc = VbglR3ReportAdditionsStatus(VBoxGuestStatusFacility_VBoxService,
454 VBoxGuestStatusCurrent_Active,
455 0 /* Flags */);
456 if (RT_FAILURE(rc))
457 VBoxServiceError("Could not report facility (%u) status %u, rc=%Rrc\n",
458 VBoxGuestStatusFacility_VBoxService, VBoxGuestStatusCurrent_Active, rc);
459
460#ifdef RT_OS_WINDOWS
461 /*
462 * Wait for the semaphore to be signalled.
463 */
464 VBoxServiceVerbose(1, "Waiting in main thread\n");
465 rc = RTSemEventCreate(&g_hEvtWindowsService);
466 AssertRC(rc);
467 while (!ASMAtomicReadBool(&g_fWindowsServiceShutdown))
468 {
469 rc = RTSemEventWait(g_hEvtWindowsService, RT_INDEFINITE_WAIT);
470 AssertRC(rc);
471 }
472 RTSemEventDestroy(g_hEvtWindowsService);
473 g_hEvtWindowsService = NIL_RTSEMEVENT;
474
475#else
476 /*
477 * Wait explicitly for a HUP, INT, QUIT, ABRT or TERM signal, blocking
478 * all important signals.
479 *
480 * The annoying EINTR/ERESTART loop is for the benefit of Solaris where
481 * sigwait returns when we receive a SIGCHLD. Kind of makes sense since
482 */
483 sigset_t signalMask;
484 sigemptyset(&signalMask);
485 sigaddset(&signalMask, SIGHUP);
486 sigaddset(&signalMask, SIGINT);
487 sigaddset(&signalMask, SIGQUIT);
488 sigaddset(&signalMask, SIGABRT);
489 sigaddset(&signalMask, SIGTERM);
490 pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
491
492 int iSignal;
493 do
494 {
495 iSignal = -1;
496 rc = sigwait(&signalMask, &iSignal);
497 }
498 while ( rc == EINTR
499# ifdef ERESTART
500 || rc == ERESTART
501# endif
502 );
503
504 VBoxServiceVerbose(3, "VBoxServiceMainWait: Received signal %d (rc=%d)\n", iSignal, rc);
505#endif /* !RT_OS_WINDOWS */
506}
507
508
509int main(int argc, char **argv)
510{
511 /*
512 * Init globals and such.
513 */
514 RTR3Init();
515
516 g_pszProgName = RTPathFilename(argv[0]);
517
518 int rc;
519#ifdef VBOXSERVICE_TOOLBOX
520 if (argc > 1)
521 {
522 /*
523 * Run toolbox code before all other stuff, especially before checking the global
524 * mutex because VBoxService might spawn itself to execute some commands.
525 */
526 int iExitCode;
527 if (VBoxServiceToolboxMain(argc - 1, &argv[1], &iExitCode))
528 return iExitCode;
529 }
530#endif
531
532 /*
533 * Connect to the kernel part before daemonizing so we can fail and
534 * complain if there is some kind of problem. We need to initialize the
535 * guest lib *before* we do the pre-init just in case one of services needs
536 * do to some initial stuff with it.
537 */
538 VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
539 rc = VbglR3Init();
540 if (RT_FAILURE(rc))
541 return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
542
543#ifdef RT_OS_WINDOWS
544 /*
545 * Check if we're the specially spawned VBoxService.exe process that
546 * handles page fusion. This saves an extra executable.
547 */
548 if ( argc == 2
549 && !strcmp(argv[1], "--pagefusionfork"))
550 return VBoxServicePageSharingInitFork();
551#endif
552
553 /*
554 * Do pre-init of services.
555 */
556 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
557 {
558 rc = g_aServices[j].pDesc->pfnPreInit();
559 if (RT_FAILURE(rc))
560 return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
561 }
562#ifdef RT_OS_WINDOWS
563 /*
564 * Make sure only one instance of VBoxService runs at a time. Create a
565 * global mutex for that. Do not use a global namespace ("Global\\") for
566 * mutex name here, will blow up NT4 compatibility!
567 */
568 /** @todo r=bird: Use Global\\ prefix or this serves no purpose on terminal servers. */
569 HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME);
570 if ( hMutexAppRunning != NULL
571 && GetLastError() == ERROR_ALREADY_EXISTS)
572 {
573 VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
574
575 /* Close the mutex for this application instance. */
576 CloseHandle(hMutexAppRunning);
577 hMutexAppRunning = NULL;
578
579 /** @todo r=bird: How does this cause us to terminate? Btw. Why do
580 * we do this before parsing parameters? 'VBoxService --help'
581 * and 'VBoxService --version' won't work now when the service
582 * is running... */
583 }
584#endif
585
586 /*
587 * Parse the arguments.
588 */
589 bool fDaemonize = true;
590 bool fDaemonized = false;
591 for (int i = 1; i < argc; i++)
592 {
593 const char *psz = argv[i];
594 if (*psz != '-')
595 return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
596 psz++;
597
598 /* translate long argument to short */
599 if (*psz == '-')
600 {
601 psz++;
602 size_t cch = strlen(psz);
603#define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
604 && !memcmp(psz, strconst, sizeof(strconst) - 1) )
605 if (MATCHES("foreground"))
606 psz = "f";
607 else if (MATCHES("verbose"))
608 psz = "v";
609 else if (MATCHES("help"))
610 psz = "h";
611 else if (MATCHES("interval"))
612 psz = "i";
613#ifdef RT_OS_WINDOWS
614 else if (MATCHES("register"))
615 psz = "r";
616 else if (MATCHES("unregister"))
617 psz = "u";
618#endif
619 else if (MATCHES("daemonized"))
620 {
621 fDaemonized = true;
622 continue;
623 }
624 else
625 {
626 bool fFound = false;
627
628 if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
629 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
630 if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
631 g_aServices[j].fEnabled = true;
632
633 if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
634 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
635 if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
636 g_aServices[j].fEnabled = false;
637
638 if (!fFound)
639 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
640 {
641 rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
642 fFound = rc == 0;
643 if (fFound)
644 break;
645 if (rc != -1)
646 return rc;
647 }
648 if (!fFound)
649 return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
650 continue;
651 }
652#undef MATCHES
653 }
654
655 /* handle the string of short options. */
656 do
657 {
658 switch (*psz)
659 {
660 case 'i':
661 rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
662 &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
663 if (rc)
664 return rc;
665 psz = NULL;
666 break;
667
668 case 'f':
669 fDaemonize = false;
670 break;
671
672 case 'v':
673 g_cVerbosity++;
674 break;
675
676 case 'h':
677 case '?':
678 return VBoxServiceUsage();
679
680#ifdef RT_OS_WINDOWS
681 case 'r':
682 return VBoxServiceWinInstall();
683
684 case 'u':
685 return VBoxServiceWinUninstall();
686#endif
687
688 default:
689 {
690 bool fFound = false;
691 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
692 {
693 rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
694 fFound = rc == 0;
695 if (fFound)
696 break;
697 if (rc != -1)
698 return rc;
699 }
700 if (!fFound)
701 return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
702 break;
703 }
704 }
705 } while (psz && *++psz);
706 }
707 /*
708 * Check that at least one service is enabled.
709 */
710 if (!VBoxServiceCheckStartedServices())
711 return VBoxServiceSyntax("At least one service must be enabled.\n");
712
713 VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
714 RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
715
716 /*
717 * Daemonize if requested.
718 */
719 RTEXITCODE rcExit;
720 if (fDaemonize && !fDaemonized)
721 {
722#ifdef RT_OS_WINDOWS
723 VBoxServiceVerbose(2, "Starting service dispatcher ...\n");
724 rcExit = VBoxServiceWinEnterCtrlDispatcher();
725#else
726 VBoxServiceVerbose(1, "Daemonizing...\n");
727 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
728 if (RT_FAILURE(rc))
729 return VBoxServiceError("Daemon failed: %Rrc\n", rc);
730 /* in-child */
731#endif
732 }
733#ifdef RT_OS_WINDOWS
734 else
735#endif
736 {
737 /*
738 * Windows: We're running the service as a console application now. Start the
739 * services, enter the main thread's run loop and stop them again
740 * when it returns.
741 *
742 * POSIX: This is used for both daemons and console runs. Start all services
743 * and return immediately.
744 */
745 rc = VBoxServiceStartServices();
746 rcExit = RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
747 if (RT_SUCCESS(rc))
748 VBoxServiceMainWait();
749 VBoxServiceStopServices();
750 }
751
752#ifdef RT_OS_WINDOWS
753 /*
754 * Release instance mutex if we got it.
755 */
756 if (hMutexAppRunning != NULL)
757 {
758 ::CloseHandle(hMutexAppRunning);
759 hMutexAppRunning = NULL;
760 }
761#endif
762
763 VBoxServiceVerbose(0, "Ended.\n");
764 return rcExit;
765}
766
Note: See TracBrowser for help on using the repository browser.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette