VirtualBox

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

Last change on this file since 34079 was 33854, checked in by vboxsync, 14 years ago

VBoxService/Control: Move the internal tool handling before VbglR3 init, needed for non-Windows guests, added some more logging.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 23.3 KB
Line 
1/* $Id: VBoxService.cpp 33854 2010-11-08 15:20:57Z 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 return rc;
366}
367
368
369/**
370 * Stops and terminates the services.
371 *
372 * This should be called even when VBoxServiceStartServices fails so it can
373 * clean up anything that we succeeded in starting.
374 */
375int VBoxServiceStopServices(void)
376{
377 int rc = VINF_SUCCESS;
378
379 /*
380 * Signal all the services.
381 */
382 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
383 ASMAtomicWriteBool(&g_aServices[j].fShutdown, true);
384
385 /*
386 * Do the pfnStop callback on all running services.
387 */
388 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
389 if (g_aServices[j].fStarted)
390 {
391 VBoxServiceVerbose(3, "Calling stop function for service '%s' ...\n", g_aServices[j].pDesc->pszName);
392 g_aServices[j].pDesc->pfnStop();
393 }
394
395 /*
396 * Wait for all the service threads to complete.
397 */
398 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
399 {
400 if (!g_aServices[j].fEnabled) /* Only stop services which were started before. */
401 continue;
402 if (g_aServices[j].Thread != NIL_RTTHREAD)
403 {
404 VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
405 for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
406 {
407 rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
408 if (RT_SUCCESS(rc))
409 break;
410#ifdef RT_OS_WINDOWS
411 /* Notify SCM that it takes a bit longer ... */
412 VBoxServiceWinSetStopPendingStatus(i + j*32);
413#endif
414 }
415 if (RT_FAILURE(rc))
416 VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
417 }
418 VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
419 g_aServices[j].pDesc->pfnTerm();
420 }
421
422#ifdef RT_OS_WINDOWS
423 /*
424 * Wake up and tell the main() thread that we're shutting down (it's
425 * sleeping in VBoxServiceMainWait).
426 */
427 if (g_hEvtWindowsService != NIL_RTSEMEVENT)
428 {
429 VBoxServiceVerbose(3, "Stopping the main thread...\n");
430 ASMAtomicWriteBool(&g_fWindowsServiceShutdown, true);
431 rc = RTSemEventSignal(g_hEvtWindowsService);
432 AssertRC(rc);
433 }
434#endif
435
436 VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc);
437 return rc;
438}
439
440
441/**
442 * Block the main thread until the service shuts down.
443 */
444void VBoxServiceMainWait(void)
445{
446 int rc;
447
448 /* Report the host that we're up and running! */
449 rc = VbglR3ReportAdditionsStatus(VBoxGuestStatusFacility_VBoxService,
450 VBoxGuestStatusCurrent_Active,
451 0); /* Flags; not used. */
452 if (RT_FAILURE(rc))
453 VBoxServiceError("Failed to report Guest Additions status to the host! rc=%Rrc\n", rc);
454
455#ifdef RT_OS_WINDOWS
456 /*
457 * Wait for the semaphore to be signalled.
458 */
459 VBoxServiceVerbose(1, "Waiting in main thread\n");
460 rc = RTSemEventCreate(&g_hEvtWindowsService);
461 AssertRC(rc);
462 while (!ASMAtomicReadBool(&g_fWindowsServiceShutdown))
463 {
464 rc = RTSemEventWait(g_hEvtWindowsService, RT_INDEFINITE_WAIT);
465 AssertRC(rc);
466 }
467 RTSemEventDestroy(g_hEvtWindowsService);
468 g_hEvtWindowsService = NIL_RTSEMEVENT;
469
470#else
471 /*
472 * Wait explicitly for a HUP, INT, QUIT, ABRT or TERM signal, blocking
473 * all important signals.
474 *
475 * The annoying EINTR/ERESTART loop is for the benefit of Solaris where
476 * sigwait returns when we receive a SIGCHLD. Kind of makes sense since
477 */
478 sigset_t signalMask;
479 sigemptyset(&signalMask);
480 sigaddset(&signalMask, SIGHUP);
481 sigaddset(&signalMask, SIGINT);
482 sigaddset(&signalMask, SIGQUIT);
483 sigaddset(&signalMask, SIGABRT);
484 sigaddset(&signalMask, SIGTERM);
485 pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
486
487 int iSignal;
488 do
489 {
490 iSignal = -1;
491 rc = sigwait(&signalMask, &iSignal);
492 }
493 while ( rc == EINTR
494# ifdef ERESTART
495 || rc == ERESTART
496# endif
497 );
498
499 VBoxServiceVerbose(3, "VBoxServiceMainWait: Received signal %d (rc=%d)\n", iSignal, rc);
500#endif /* !RT_OS_WINDOWS */
501}
502
503
504int main(int argc, char **argv)
505{
506 /*
507 * Init globals and such.
508 */
509 RTR3Init();
510
511 g_pszProgName = RTPathFilename(argv[0]);
512
513 int rc;
514#ifdef VBOXSERVICE_TOOLBOX
515 if (argc > 1)
516 {
517 /*
518 * Run toolbox code before all other stuff, especially before checking the global
519 * mutex because VBoxService might spawn itself to execute some commands.
520 */
521 rc = VBoxServiceToolboxMain(argc - 1, &argv[1]);
522 if (rc != VERR_NOT_FOUND) /* Internal tool found? Then bail out. */
523 return rc;
524 }
525#endif
526
527 /*
528 * Connect to the kernel part before daemonizing so we can fail and
529 * complain if there is some kind of problem. We need to initialize the
530 * guest lib *before* we do the pre-init just in case one of services needs
531 * do to some initial stuff with it.
532 */
533 VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
534 rc = VbglR3Init();
535 if (RT_FAILURE(rc))
536 return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
537
538#ifdef RT_OS_WINDOWS
539 /*
540 * Check if we're the specially spawned VBoxService.exe process that
541 * handles page fusion. This saves an extra executable.
542 */
543 if ( argc == 2
544 && !strcmp(argv[1], "--pagefusionfork"))
545 return VBoxServicePageSharingInitFork();
546#endif
547
548 /*
549 * Do pre-init of services.
550 */
551 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
552 {
553 rc = g_aServices[j].pDesc->pfnPreInit();
554 if (RT_FAILURE(rc))
555 return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
556 }
557#ifdef RT_OS_WINDOWS
558 /*
559 * Make sure only one instance of VBoxService runs at a time. Create a
560 * global mutex for that. Do not use a global namespace ("Global\\") for
561 * mutex name here, will blow up NT4 compatibility!
562 */
563 /** @todo r=bird: Use Global\\ prefix or this serves no purpose on terminal servers. */
564 HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME);
565 if ( hMutexAppRunning != NULL
566 && GetLastError() == ERROR_ALREADY_EXISTS)
567 {
568 VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
569
570 /* Close the mutex for this application instance. */
571 CloseHandle(hMutexAppRunning);
572 hMutexAppRunning = NULL;
573
574 /** @todo r=bird: How does this cause us to terminate? Btw. Why do
575 * we do this before parsing parameters? 'VBoxService --help'
576 * and 'VBoxService --version' won't work now when the service
577 * is running... */
578 }
579#endif
580
581 /*
582 * Parse the arguments.
583 */
584 bool fDaemonize = true;
585 bool fDaemonized = false;
586 for (int i = 1; i < argc; i++)
587 {
588 const char *psz = argv[i];
589 if (*psz != '-')
590 return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
591 psz++;
592
593 /* translate long argument to short */
594 if (*psz == '-')
595 {
596 psz++;
597 size_t cch = strlen(psz);
598#define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
599 && !memcmp(psz, strconst, sizeof(strconst) - 1) )
600 if (MATCHES("foreground"))
601 psz = "f";
602 else if (MATCHES("verbose"))
603 psz = "v";
604 else if (MATCHES("help"))
605 psz = "h";
606 else if (MATCHES("interval"))
607 psz = "i";
608#ifdef RT_OS_WINDOWS
609 else if (MATCHES("register"))
610 psz = "r";
611 else if (MATCHES("unregister"))
612 psz = "u";
613#endif
614 else if (MATCHES("daemonized"))
615 {
616 fDaemonized = true;
617 continue;
618 }
619 else
620 {
621 bool fFound = false;
622
623 if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
624 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
625 if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
626 g_aServices[j].fEnabled = true;
627
628 if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
629 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
630 if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
631 g_aServices[j].fEnabled = false;
632
633 if (!fFound)
634 for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
635 {
636 rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
637 fFound = rc == 0;
638 if (fFound)
639 break;
640 if (rc != -1)
641 return rc;
642 }
643 if (!fFound)
644 return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
645 continue;
646 }
647#undef MATCHES
648 }
649
650 /* handle the string of short options. */
651 do
652 {
653 switch (*psz)
654 {
655 case 'i':
656 rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
657 &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
658 if (rc)
659 return rc;
660 psz = NULL;
661 break;
662
663 case 'f':
664 fDaemonize = false;
665 break;
666
667 case 'v':
668 g_cVerbosity++;
669 break;
670
671 case 'h':
672 case '?':
673 return VBoxServiceUsage();
674
675#ifdef RT_OS_WINDOWS
676 case 'r':
677 return VBoxServiceWinInstall();
678
679 case 'u':
680 return VBoxServiceWinUninstall();
681#endif
682
683 default:
684 {
685 bool fFound = false;
686 for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
687 {
688 rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
689 fFound = rc == 0;
690 if (fFound)
691 break;
692 if (rc != -1)
693 return rc;
694 }
695 if (!fFound)
696 return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
697 break;
698 }
699 }
700 } while (psz && *++psz);
701 }
702 /*
703 * Check that at least one service is enabled.
704 */
705 if (!VBoxServiceCheckStartedServices())
706 return VBoxServiceSyntax("At least one service must be enabled.\n");
707
708 VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
709 RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
710
711 /*
712 * Daemonize if requested.
713 */
714 RTEXITCODE rcExit;
715 if (fDaemonize && !fDaemonized)
716 {
717#ifdef RT_OS_WINDOWS
718 VBoxServiceVerbose(2, "Starting service dispatcher ...\n");
719 rcExit = VBoxServiceWinEnterCtrlDispatcher();
720#else
721 VBoxServiceVerbose(1, "Daemonizing...\n");
722 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
723 if (RT_FAILURE(rc))
724 return VBoxServiceError("Daemon failed: %Rrc\n", rc);
725 /* in-child */
726#endif
727 }
728#ifdef RT_OS_WINDOWS
729 else
730#endif
731 {
732 /*
733 * Windows: We're running the service as a console application now. Start the
734 * services, enter the main thread's run loop and stop them again
735 * when it returns.
736 *
737 * POSIX: This is used for both daemons and console runs. Start all services
738 * and return immediately.
739 */
740 rc = VBoxServiceStartServices();
741 rcExit = RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
742 if (RT_SUCCESS(rc))
743 VBoxServiceMainWait();
744 VBoxServiceStopServices();
745 }
746
747#ifdef RT_OS_WINDOWS
748 /*
749 * Release instance mutex if we got it.
750 */
751 if (hMutexAppRunning != NULL)
752 {
753 ::CloseHandle(hMutexAppRunning);
754 hMutexAppRunning = NULL;
755 }
756#endif
757
758 VBoxServiceVerbose(0, "Ended.\n");
759 return rcExit;
760}
761
Note: See TracBrowser for help on using the repository browser.

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