1 | /* $Id: VBoxService.cpp 33395 2010-10-24 16:18:01Z 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]). */
|
---|
58 | char *g_pszProgName = (char *)"";
|
---|
59 | /** The current verbosity level. */
|
---|
60 | int g_cVerbosity = 0;
|
---|
61 | /** The default service interval (the -i | --interval) option). */
|
---|
62 | uint32_t g_DefaultInterval = 0;
|
---|
63 | #ifdef RT_OS_WINDOWS
|
---|
64 | /** Signal shutdown to the Windows service thread. */
|
---|
65 | static bool volatile g_fWindowsServiceShutdown;
|
---|
66 | /** Event the Windows service thread waits for shutdown. */
|
---|
67 | static RTSEMEVENT g_hEvtWindowsService;
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | /**
|
---|
71 | * The details of the services that has been compiled in.
|
---|
72 | */
|
---|
73 | static 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 | */
|
---|
124 | static 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 daemonzie 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 | */
|
---|
170 | RTEXITCODE 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 | */
|
---|
190 | RTEXITCODE 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 | */
|
---|
214 | void 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 | */
|
---|
242 | int 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 secconds 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 | */
|
---|
271 | static 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 | */
|
---|
294 | static 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 | */
|
---|
309 | int 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 | */
|
---|
375 | int 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 | */
|
---|
444 | void 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 |
|
---|
504 | int main(int argc, char **argv)
|
---|
505 | {
|
---|
506 | /*
|
---|
507 | * Init globals and such.
|
---|
508 | */
|
---|
509 | RTR3Init();
|
---|
510 |
|
---|
511 | /*
|
---|
512 | * Connect to the kernel part before daemonizing so we can fail and
|
---|
513 | * complain if there is some kind of problem. We need to initialize the
|
---|
514 | * guest lib *before* we do the pre-init just in case one of services needs
|
---|
515 | * do to some initial stuff with it.
|
---|
516 | */
|
---|
517 | VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
|
---|
518 | int rc = VbglR3Init();
|
---|
519 | if (RT_FAILURE(rc))
|
---|
520 | return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
|
---|
521 |
|
---|
522 | #ifdef RT_OS_WINDOWS
|
---|
523 | /*
|
---|
524 | * Check if we're the specially spawned VBoxService.exe process that
|
---|
525 | * handles page fusion. This saves an extra executable.
|
---|
526 | */
|
---|
527 | if ( argc == 2
|
---|
528 | && !strcmp(argv[1], "--pagefusionfork"))
|
---|
529 | return VBoxServicePageSharingInitFork();
|
---|
530 | #endif
|
---|
531 |
|
---|
532 | g_pszProgName = RTPathFilename(argv[0]);
|
---|
533 |
|
---|
534 | #ifdef VBOXSERVICE_TOOLBOX
|
---|
535 | if (argc > 1)
|
---|
536 | {
|
---|
537 | /*
|
---|
538 | * Run toolbox code before all other stuff, especially before checking the global
|
---|
539 | * mutex because VBoxService might spawn itself to execute some commands.
|
---|
540 | */
|
---|
541 | rc = VBoxServiceToolboxMain(argc - 1, &argv[1]);
|
---|
542 | if (rc != VERR_NOT_FOUND) /* Internal tool found? Then bail out. */
|
---|
543 | return rc;
|
---|
544 | }
|
---|
545 | #endif
|
---|
546 |
|
---|
547 | /*
|
---|
548 | * Do pre-init of services.
|
---|
549 | */
|
---|
550 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
551 | {
|
---|
552 | rc = g_aServices[j].pDesc->pfnPreInit();
|
---|
553 | if (RT_FAILURE(rc))
|
---|
554 | return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
|
---|
555 | }
|
---|
556 | #ifdef RT_OS_WINDOWS
|
---|
557 | /*
|
---|
558 | * Make sure only one instance of VBoxService runs at a time. Create a
|
---|
559 | * global mutex for that. Do not use a global namespace ("Global\\") for
|
---|
560 | * mutex name here, will blow up NT4 compatibility!
|
---|
561 | */
|
---|
562 | /** @todo r=bird: Use Global\\ prefix or this serves no purpose on terminal servers. */
|
---|
563 | HANDLE hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME);
|
---|
564 | if ( hMutexAppRunning != NULL
|
---|
565 | && GetLastError() == ERROR_ALREADY_EXISTS)
|
---|
566 | {
|
---|
567 | VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
|
---|
568 |
|
---|
569 | /* Close the mutex for this application instance. */
|
---|
570 | CloseHandle(hMutexAppRunning);
|
---|
571 | hMutexAppRunning = NULL;
|
---|
572 |
|
---|
573 | /** @todo r=bird: How does this cause us to terminate? Btw. Why do
|
---|
574 | * we do this before parsing parameters? 'VBoxService --help'
|
---|
575 | * and 'VBoxService --version' won't work now when the service
|
---|
576 | * is running... */
|
---|
577 | }
|
---|
578 | #endif
|
---|
579 |
|
---|
580 | /*
|
---|
581 | * Parse the arguments.
|
---|
582 | */
|
---|
583 | bool fDaemonize = true;
|
---|
584 | bool fDaemonized = false;
|
---|
585 | for (int i = 1; i < argc; i++)
|
---|
586 | {
|
---|
587 | const char *psz = argv[i];
|
---|
588 | if (*psz != '-')
|
---|
589 | return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
|
---|
590 | psz++;
|
---|
591 |
|
---|
592 | /* translate long argument to short */
|
---|
593 | if (*psz == '-')
|
---|
594 | {
|
---|
595 | psz++;
|
---|
596 | size_t cch = strlen(psz);
|
---|
597 | #define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
|
---|
598 | && !memcmp(psz, strconst, sizeof(strconst) - 1) )
|
---|
599 | if (MATCHES("foreground"))
|
---|
600 | psz = "f";
|
---|
601 | else if (MATCHES("verbose"))
|
---|
602 | psz = "v";
|
---|
603 | else if (MATCHES("help"))
|
---|
604 | psz = "h";
|
---|
605 | else if (MATCHES("interval"))
|
---|
606 | psz = "i";
|
---|
607 | #ifdef RT_OS_WINDOWS
|
---|
608 | else if (MATCHES("register"))
|
---|
609 | psz = "r";
|
---|
610 | else if (MATCHES("unregister"))
|
---|
611 | psz = "u";
|
---|
612 | #endif
|
---|
613 | else if (MATCHES("daemonized"))
|
---|
614 | {
|
---|
615 | fDaemonized = true;
|
---|
616 | continue;
|
---|
617 | }
|
---|
618 | else
|
---|
619 | {
|
---|
620 | bool fFound = false;
|
---|
621 |
|
---|
622 | if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
|
---|
623 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
624 | if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
625 | g_aServices[j].fEnabled = true;
|
---|
626 |
|
---|
627 | if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
|
---|
628 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
629 | if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
630 | g_aServices[j].fEnabled = false;
|
---|
631 |
|
---|
632 | if (!fFound)
|
---|
633 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
634 | {
|
---|
635 | rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
|
---|
636 | fFound = rc == 0;
|
---|
637 | if (fFound)
|
---|
638 | break;
|
---|
639 | if (rc != -1)
|
---|
640 | return rc;
|
---|
641 | }
|
---|
642 | if (!fFound)
|
---|
643 | return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
|
---|
644 | continue;
|
---|
645 | }
|
---|
646 | #undef MATCHES
|
---|
647 | }
|
---|
648 |
|
---|
649 | /* handle the string of short options. */
|
---|
650 | do
|
---|
651 | {
|
---|
652 | switch (*psz)
|
---|
653 | {
|
---|
654 | case 'i':
|
---|
655 | rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
|
---|
656 | &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
|
---|
657 | if (rc)
|
---|
658 | return rc;
|
---|
659 | psz = NULL;
|
---|
660 | break;
|
---|
661 |
|
---|
662 | case 'f':
|
---|
663 | fDaemonize = false;
|
---|
664 | break;
|
---|
665 |
|
---|
666 | case 'v':
|
---|
667 | g_cVerbosity++;
|
---|
668 | break;
|
---|
669 |
|
---|
670 | case 'h':
|
---|
671 | case '?':
|
---|
672 | return VBoxServiceUsage();
|
---|
673 |
|
---|
674 | #ifdef RT_OS_WINDOWS
|
---|
675 | case 'r':
|
---|
676 | return VBoxServiceWinInstall();
|
---|
677 |
|
---|
678 | case 'u':
|
---|
679 | return VBoxServiceWinUninstall();
|
---|
680 | #endif
|
---|
681 |
|
---|
682 | default:
|
---|
683 | {
|
---|
684 | bool fFound = false;
|
---|
685 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
686 | {
|
---|
687 | rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
|
---|
688 | fFound = rc == 0;
|
---|
689 | if (fFound)
|
---|
690 | break;
|
---|
691 | if (rc != -1)
|
---|
692 | return rc;
|
---|
693 | }
|
---|
694 | if (!fFound)
|
---|
695 | return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
|
---|
696 | break;
|
---|
697 | }
|
---|
698 | }
|
---|
699 | } while (psz && *++psz);
|
---|
700 | }
|
---|
701 | /*
|
---|
702 | * Check that at least one service is enabled.
|
---|
703 | */
|
---|
704 | if (!VBoxServiceCheckStartedServices())
|
---|
705 | return VBoxServiceSyntax("At least one service must be enabled.\n");
|
---|
706 |
|
---|
707 | VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
|
---|
708 | RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
|
---|
709 |
|
---|
710 | /*
|
---|
711 | * Daemonize if requested.
|
---|
712 | */
|
---|
713 | RTEXITCODE rcExit;
|
---|
714 | if (fDaemonize && !fDaemonized)
|
---|
715 | {
|
---|
716 | #ifdef RT_OS_WINDOWS
|
---|
717 | VBoxServiceVerbose(2, "Starting service dispatcher ...\n");
|
---|
718 | rcExit = VBoxServiceWinEnterCtrlDispatcher();
|
---|
719 | #else
|
---|
720 | VBoxServiceVerbose(1, "Daemonizing...\n");
|
---|
721 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
|
---|
722 | if (RT_FAILURE(rc))
|
---|
723 | return VBoxServiceError("Daemon failed: %Rrc\n", rc);
|
---|
724 | /* in-child */
|
---|
725 | #endif
|
---|
726 | }
|
---|
727 | #ifdef RT_OS_WINDOWS
|
---|
728 | else
|
---|
729 | #endif
|
---|
730 | {
|
---|
731 | /*
|
---|
732 | * Windows: We're running the service as a console application now. Start the
|
---|
733 | * services, enter the main thread's run loop and stop them again
|
---|
734 | * when it returns.
|
---|
735 | *
|
---|
736 | * POSIX: This is used for both daemons and console runs. Start all services
|
---|
737 | * and return immediately.
|
---|
738 | */
|
---|
739 | rc = VBoxServiceStartServices();
|
---|
740 | rcExit = RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
741 | if (RT_SUCCESS(rc))
|
---|
742 | VBoxServiceMainWait();
|
---|
743 | VBoxServiceStopServices();
|
---|
744 | }
|
---|
745 |
|
---|
746 | #ifdef RT_OS_WINDOWS
|
---|
747 | /*
|
---|
748 | * Release instance mutex if we got it.
|
---|
749 | */
|
---|
750 | if (hMutexAppRunning != NULL)
|
---|
751 | {
|
---|
752 | ::CloseHandle(hMutexAppRunning);
|
---|
753 | hMutexAppRunning = NULL;
|
---|
754 | }
|
---|
755 | #endif
|
---|
756 |
|
---|
757 | VBoxServiceVerbose(0, "Ended.\n");
|
---|
758 | return rcExit;
|
---|
759 | }
|
---|
760 |
|
---|