1 | /* $Id: VBoxService.cpp 28967 2010-05-03 11:44:28Z 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 | #endif
|
---|
31 |
|
---|
32 | #include "product-generated.h"
|
---|
33 | #include <iprt/asm.h>
|
---|
34 | #include <iprt/buildconfig.h>
|
---|
35 | #include <iprt/initterm.h>
|
---|
36 | #include <iprt/path.h>
|
---|
37 | #include <iprt/string.h>
|
---|
38 | #include <iprt/stream.h>
|
---|
39 | #include <iprt/thread.h>
|
---|
40 |
|
---|
41 | #include <VBox/VBoxGuestLib.h>
|
---|
42 | #include <VBox/log.h>
|
---|
43 |
|
---|
44 | #include "VBoxServiceInternal.h"
|
---|
45 |
|
---|
46 |
|
---|
47 | /*******************************************************************************
|
---|
48 | * Global Variables *
|
---|
49 | *******************************************************************************/
|
---|
50 | /** The program name (derived from argv[0]). */
|
---|
51 | char *g_pszProgName = (char *)"";
|
---|
52 | /** The current verbosity level. */
|
---|
53 | int g_cVerbosity = 0;
|
---|
54 | /** The default service interval (the -i | --interval) option). */
|
---|
55 | uint32_t g_DefaultInterval = 0;
|
---|
56 | /** Shutdown the main thread. (later, for signals.) */
|
---|
57 | bool volatile g_fShutdown;
|
---|
58 |
|
---|
59 | /**
|
---|
60 | * The details of the services that has been compiled in.
|
---|
61 | */
|
---|
62 | static struct
|
---|
63 | {
|
---|
64 | /** Pointer to the service descriptor. */
|
---|
65 | PCVBOXSERVICE pDesc;
|
---|
66 | /** The worker thread. NIL_RTTHREAD if it's the main thread. */
|
---|
67 | RTTHREAD Thread;
|
---|
68 | /** Shutdown indicator. */
|
---|
69 | bool volatile fShutdown;
|
---|
70 | /** Indicator set by the service thread exiting. */
|
---|
71 | bool volatile fStopped;
|
---|
72 | /** Whether the service was started or not. */
|
---|
73 | bool fStarted;
|
---|
74 | /** Whether the service is enabled or not. */
|
---|
75 | bool fEnabled;
|
---|
76 | } g_aServices[] =
|
---|
77 | {
|
---|
78 | #ifdef VBOXSERVICE_CONTROL
|
---|
79 | { &g_Control, NIL_RTTHREAD, false, false, false, true },
|
---|
80 | #endif
|
---|
81 | #ifdef VBOXSERVICE_TIMESYNC
|
---|
82 | { &g_TimeSync, NIL_RTTHREAD, false, false, false, true },
|
---|
83 | #endif
|
---|
84 | #ifdef VBOXSERVICE_CLIPBOARD
|
---|
85 | { &g_Clipboard, NIL_RTTHREAD, false, false, false, true },
|
---|
86 | #endif
|
---|
87 | #ifdef VBOXSERVICE_VMINFO
|
---|
88 | { &g_VMInfo, NIL_RTTHREAD, false, false, false, true },
|
---|
89 | #endif
|
---|
90 | #ifdef VBOXSERVICE_EXEC
|
---|
91 | { &g_Exec, NIL_RTTHREAD, false, false, false, true },
|
---|
92 | #endif
|
---|
93 | #ifdef VBOXSERVICE_CPUHOTPLUG
|
---|
94 | { &g_CpuHotPlug, NIL_RTTHREAD, false, false, false, true },
|
---|
95 | #endif
|
---|
96 | #ifdef VBOXSERVICE_MANAGEMENT
|
---|
97 | #ifdef VBOX_WITH_MEMBALLOON
|
---|
98 | { &g_MemBalloon, NIL_RTTHREAD, false, false, false, true },
|
---|
99 | #endif
|
---|
100 | { &g_VMStatistics, NIL_RTTHREAD, false, false, false, true },
|
---|
101 | #endif
|
---|
102 | };
|
---|
103 |
|
---|
104 |
|
---|
105 | /**
|
---|
106 | * Displays the program usage message.
|
---|
107 | *
|
---|
108 | * @returns 1.
|
---|
109 | */
|
---|
110 | static int VBoxServiceUsage(void)
|
---|
111 | {
|
---|
112 | RTPrintf("usage: %s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
|
---|
113 | " [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
|
---|
114 | #ifdef RT_OS_WINDOWS
|
---|
115 | RTPrintf(" [-r|--register] [-u|--unregister]\n");
|
---|
116 | #endif
|
---|
117 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
118 | RTPrintf(" %s\n", g_aServices[j].pDesc->pszUsage);
|
---|
119 | RTPrintf("\n"
|
---|
120 | "Options:\n"
|
---|
121 | " -i | --interval The default interval.\n"
|
---|
122 | " -f | --foreground Don't daemonzie the program. For debugging.\n"
|
---|
123 | " -v | --verbose Increment the verbosity level. For debugging.\n"
|
---|
124 | " -h | -? | --help Show this message and exit with status 1.\n"
|
---|
125 | );
|
---|
126 | #ifdef RT_OS_WINDOWS
|
---|
127 | RTPrintf(" -r | --register Installs the service.\n"
|
---|
128 | " -u | --unregister Uninstall service.\n");
|
---|
129 | #endif
|
---|
130 |
|
---|
131 | RTPrintf("\n"
|
---|
132 | "Service specific options:\n");
|
---|
133 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
134 | {
|
---|
135 | RTPrintf(" --enable-%-10s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
136 | RTPrintf(" --disable-%-9s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
137 | if (g_aServices[j].pDesc->pszOptions)
|
---|
138 | RTPrintf("%s", g_aServices[j].pDesc->pszOptions);
|
---|
139 | }
|
---|
140 | RTPrintf("\n"
|
---|
141 | " Copyright (C) 2009-" VBOX_C_YEAR " " VBOX_VENDOR "\n");
|
---|
142 |
|
---|
143 | return 1;
|
---|
144 | }
|
---|
145 |
|
---|
146 |
|
---|
147 | /**
|
---|
148 | * Displays a syntax error message.
|
---|
149 | *
|
---|
150 | * @returns 1
|
---|
151 | * @param pszFormat The message text.
|
---|
152 | * @param ... Format arguments.
|
---|
153 | */
|
---|
154 | int VBoxServiceSyntax(const char *pszFormat, ...)
|
---|
155 | {
|
---|
156 | RTStrmPrintf(g_pStdErr, "%s: syntax error: ", g_pszProgName);
|
---|
157 |
|
---|
158 | va_list va;
|
---|
159 | va_start(va, pszFormat);
|
---|
160 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
161 | va_end(va);
|
---|
162 |
|
---|
163 | return 1;
|
---|
164 | }
|
---|
165 |
|
---|
166 |
|
---|
167 | /**
|
---|
168 | * Displays an error message.
|
---|
169 | *
|
---|
170 | * @returns 1
|
---|
171 | * @param pszFormat The message text.
|
---|
172 | * @param ... Format arguments.
|
---|
173 | */
|
---|
174 | int VBoxServiceError(const char *pszFormat, ...)
|
---|
175 | {
|
---|
176 | RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
|
---|
177 |
|
---|
178 | va_list va;
|
---|
179 | va_start(va, pszFormat);
|
---|
180 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
181 | va_end(va);
|
---|
182 |
|
---|
183 | va_start(va, pszFormat);
|
---|
184 | LogRel(("%s: Error: %N", g_pszProgName, pszFormat, &va));
|
---|
185 | va_end(va);
|
---|
186 |
|
---|
187 | return 1;
|
---|
188 | }
|
---|
189 |
|
---|
190 |
|
---|
191 | /**
|
---|
192 | * Displays a verbose message.
|
---|
193 | *
|
---|
194 | * @returns 1
|
---|
195 | * @param pszFormat The message text.
|
---|
196 | * @param ... Format arguments.
|
---|
197 | */
|
---|
198 | void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...)
|
---|
199 | {
|
---|
200 | if (iLevel <= g_cVerbosity)
|
---|
201 | {
|
---|
202 | RTStrmPrintf(g_pStdOut, "%s: ", g_pszProgName);
|
---|
203 | va_list va;
|
---|
204 | va_start(va, pszFormat);
|
---|
205 | RTStrmPrintfV(g_pStdOut, pszFormat, va);
|
---|
206 | va_end(va);
|
---|
207 |
|
---|
208 | va_start(va, pszFormat);
|
---|
209 | LogRel(("%s: %N", g_pszProgName, pszFormat, &va));
|
---|
210 | va_end(va);
|
---|
211 | }
|
---|
212 | }
|
---|
213 |
|
---|
214 |
|
---|
215 | /**
|
---|
216 | * Gets a 32-bit value argument.
|
---|
217 | *
|
---|
218 | * @returns 0 on success, non-zero exit code on error.
|
---|
219 | * @param argc The argument count.
|
---|
220 | * @param argv The argument vector
|
---|
221 | * @param psz Where in *pi to start looking for the value argument.
|
---|
222 | * @param pi Where to find and perhaps update the argument index.
|
---|
223 | * @param pu32 Where to store the 32-bit value.
|
---|
224 | * @param u32Min The minimum value.
|
---|
225 | * @param u32Max The maximum value.
|
---|
226 | */
|
---|
227 | int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
|
---|
228 | {
|
---|
229 | if (*psz == ':' || *psz == '=')
|
---|
230 | psz++;
|
---|
231 | if (!*psz)
|
---|
232 | {
|
---|
233 | if (*pi + 1 >= argc)
|
---|
234 | return VBoxServiceSyntax("Missing value for the '%s' argument\n", argv[*pi]);
|
---|
235 | psz = argv[++*pi];
|
---|
236 | }
|
---|
237 |
|
---|
238 | char *pszNext;
|
---|
239 | int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
|
---|
240 | if (RT_FAILURE(rc) || *pszNext)
|
---|
241 | return VBoxServiceSyntax("Failed to convert interval '%s' to a number.\n", psz);
|
---|
242 | if (*pu32 < u32Min || *pu32 > u32Max)
|
---|
243 | return VBoxServiceSyntax("The timesync interval of %RU32 secconds is out of range [%RU32..%RU32].\n",
|
---|
244 | *pu32, u32Min, u32Max);
|
---|
245 | return 0;
|
---|
246 | }
|
---|
247 |
|
---|
248 |
|
---|
249 | /**
|
---|
250 | * The service thread.
|
---|
251 | *
|
---|
252 | * @returns Whatever the worker function returns.
|
---|
253 | * @param ThreadSelf My thread handle.
|
---|
254 | * @param pvUser The service index.
|
---|
255 | */
|
---|
256 | static DECLCALLBACK(int) VBoxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
257 | {
|
---|
258 | const unsigned i = (uintptr_t)pvUser;
|
---|
259 |
|
---|
260 | #ifndef RT_OS_WINDOWS
|
---|
261 | /*
|
---|
262 | * Block all signals for this thread. Only the main thread will handle signals.
|
---|
263 | */
|
---|
264 | sigset_t signalMask;
|
---|
265 | sigfillset(&signalMask);
|
---|
266 | pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
|
---|
267 | #endif
|
---|
268 |
|
---|
269 | int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
|
---|
270 | ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
|
---|
271 | RTThreadUserSignal(ThreadSelf);
|
---|
272 | return rc;
|
---|
273 | }
|
---|
274 |
|
---|
275 |
|
---|
276 | unsigned VBoxServiceGetStartedServices(void)
|
---|
277 | {
|
---|
278 | unsigned iMain = ~0U;
|
---|
279 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
280 | if (g_aServices[j].fEnabled)
|
---|
281 | {
|
---|
282 | iMain = j;
|
---|
283 | break;
|
---|
284 | }
|
---|
285 |
|
---|
286 | return iMain; /* Return the index of the main service (must always come last!). */
|
---|
287 | }
|
---|
288 |
|
---|
289 | /**
|
---|
290 | * Starts the service.
|
---|
291 | *
|
---|
292 | * @returns VBox status code, errors are fully bitched.
|
---|
293 | *
|
---|
294 | * @param iMain The index of the service that belongs to the main
|
---|
295 | * thread. Pass ~0U if none does.
|
---|
296 | */
|
---|
297 | int VBoxServiceStartServices(unsigned iMain)
|
---|
298 | {
|
---|
299 | int rc;
|
---|
300 | /*
|
---|
301 | * Initialize the services.
|
---|
302 | */
|
---|
303 | VBoxServiceVerbose(2, "Initializing services ...\n");
|
---|
304 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
305 | if (g_aServices[j].fEnabled)
|
---|
306 | {
|
---|
307 | rc = g_aServices[j].pDesc->pfnInit();
|
---|
308 | if (RT_FAILURE(rc))
|
---|
309 | {
|
---|
310 | VBoxServiceError("Service '%s' failed to initialize: %Rrc\n",
|
---|
311 | g_aServices[j].pDesc->pszName, rc);
|
---|
312 | return rc;
|
---|
313 | }
|
---|
314 | }
|
---|
315 | /*
|
---|
316 | * Start the service(s).
|
---|
317 | */
|
---|
318 | VBoxServiceVerbose(2, "Starting services ...\n");
|
---|
319 | rc = VINF_SUCCESS;
|
---|
320 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
321 | {
|
---|
322 | if ( !g_aServices[j].fEnabled
|
---|
323 | || j == iMain)
|
---|
324 | continue;
|
---|
325 |
|
---|
326 | VBoxServiceVerbose(2, "Starting service '%s' ...\n", g_aServices[j].pDesc->pszName);
|
---|
327 | rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
|
---|
328 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
|
---|
329 | if (RT_FAILURE(rc))
|
---|
330 | {
|
---|
331 | VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
|
---|
332 | break;
|
---|
333 | }
|
---|
334 | g_aServices[j].fStarted = true;
|
---|
335 |
|
---|
336 | /* wait for the thread to initialize */
|
---|
337 | RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
|
---|
338 | if (g_aServices[j].fShutdown)
|
---|
339 | {
|
---|
340 | VBoxServiceError("Service '%s' failed to start!\n", g_aServices[j].pDesc->pszName);
|
---|
341 | rc = VERR_GENERAL_FAILURE;
|
---|
342 | }
|
---|
343 | }
|
---|
344 | if ( RT_SUCCESS(rc)
|
---|
345 | && iMain != ~0U)
|
---|
346 | {
|
---|
347 | /* The final service runs in the main thread. */
|
---|
348 | VBoxServiceVerbose(1, "Starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
|
---|
349 | rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
|
---|
350 | if (rc != VINF_SUCCESS) /* Only complain if service returned an error. Otherwise the service is a one-timer. */
|
---|
351 | {
|
---|
352 | VBoxServiceError("Service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
|
---|
353 | }
|
---|
354 | }
|
---|
355 | return rc;
|
---|
356 | }
|
---|
357 |
|
---|
358 |
|
---|
359 | /**
|
---|
360 | * Stops and terminates the services.
|
---|
361 | *
|
---|
362 | * This should be called even when VBoxServiceStartServices fails so it can
|
---|
363 | * clean up anything that we succeeded in starting.
|
---|
364 | */
|
---|
365 | int VBoxServiceStopServices(void)
|
---|
366 | {
|
---|
367 | int rc = VINF_SUCCESS;
|
---|
368 |
|
---|
369 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
370 | ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
|
---|
371 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
372 | if (g_aServices[j].fStarted)
|
---|
373 | g_aServices[j].pDesc->pfnStop();
|
---|
374 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
375 | if (g_aServices[j].fEnabled)
|
---|
376 | {
|
---|
377 | if (g_aServices[j].Thread != NIL_RTTHREAD)
|
---|
378 | {
|
---|
379 | VBoxServiceVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
|
---|
380 | for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
|
---|
381 | {
|
---|
382 | rc = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
|
---|
383 | if (RT_SUCCESS(rc))
|
---|
384 | break;
|
---|
385 | #ifdef RT_OS_WINDOWS
|
---|
386 | /* Notify SCM that it takes a bit longer ... */
|
---|
387 | VBoxServiceWinSetStatus(SERVICE_STOP_PENDING, i);
|
---|
388 | #endif
|
---|
389 | }
|
---|
390 | if (RT_FAILURE(rc))
|
---|
391 | VBoxServiceError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
|
---|
392 | }
|
---|
393 | VBoxServiceVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
|
---|
394 | g_aServices[j].pDesc->pfnTerm();
|
---|
395 | }
|
---|
396 |
|
---|
397 | VBoxServiceVerbose(2, "Stopping services returned: rc=%Rrc\n", rc);
|
---|
398 | return rc;
|
---|
399 | }
|
---|
400 |
|
---|
401 | #ifndef RT_OS_WINDOWS
|
---|
402 | /*
|
---|
403 | * Block all important signals, then explicitly wait until one of these signal arrives.
|
---|
404 | */
|
---|
405 | static void VBoxServiceWaitSignal(void)
|
---|
406 | {
|
---|
407 | sigset_t signalMask;
|
---|
408 | int iSignal;
|
---|
409 | sigemptyset(&signalMask);
|
---|
410 | sigaddset(&signalMask, SIGHUP);
|
---|
411 | sigaddset(&signalMask, SIGINT);
|
---|
412 | sigaddset(&signalMask, SIGQUIT);
|
---|
413 | sigaddset(&signalMask, SIGABRT);
|
---|
414 | sigaddset(&signalMask, SIGTERM);
|
---|
415 | pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
|
---|
416 | sigwait(&signalMask, &iSignal);
|
---|
417 | VBoxServiceVerbose(3, "VBoxServiceWaitSignal: Received signal %d\n", iSignal);
|
---|
418 | }
|
---|
419 | #endif
|
---|
420 |
|
---|
421 |
|
---|
422 | int main(int argc, char **argv)
|
---|
423 | {
|
---|
424 | int rc = VINF_SUCCESS;
|
---|
425 | /*
|
---|
426 | * Init globals and such.
|
---|
427 | */
|
---|
428 | RTR3Init();
|
---|
429 |
|
---|
430 | /*
|
---|
431 | * Connect to the kernel part before daemonizing so we can fail
|
---|
432 | * and complain if there is some kind of problem. We need to initialize
|
---|
433 | * the guest lib *before* we do the pre-init just in case one of services
|
---|
434 | * needs do to some initial stuff with it.
|
---|
435 | */
|
---|
436 | VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
|
---|
437 | rc = VbglR3Init();
|
---|
438 | if (RT_FAILURE(rc))
|
---|
439 | return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
|
---|
440 |
|
---|
441 | /* Do pre-init of services. */
|
---|
442 | g_pszProgName = RTPathFilename(argv[0]);
|
---|
443 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
444 | {
|
---|
445 | rc = g_aServices[j].pDesc->pfnPreInit();
|
---|
446 | if (RT_FAILURE(rc))
|
---|
447 | return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
|
---|
448 | }
|
---|
449 |
|
---|
450 | #ifdef RT_OS_WINDOWS
|
---|
451 | /* Make sure only one instance of VBoxService runs at a time. Create a global mutex for that.
|
---|
452 | Do not use a global namespace ("Global\\") for mutex name here, will blow up NT4 compatibility! */
|
---|
453 | HANDLE hMutexAppRunning = CreateMutex (NULL, FALSE, VBOXSERVICE_NAME);
|
---|
454 | if ( hMutexAppRunning != NULL
|
---|
455 | && GetLastError() == ERROR_ALREADY_EXISTS)
|
---|
456 | {
|
---|
457 | VBoxServiceError("%s is already running! Terminating.", g_pszProgName);
|
---|
458 |
|
---|
459 | /* Close the mutex for this application instance. */
|
---|
460 | CloseHandle(hMutexAppRunning);
|
---|
461 | hMutexAppRunning = NULL;
|
---|
462 | }
|
---|
463 | #endif
|
---|
464 |
|
---|
465 | /*
|
---|
466 | * Parse the arguments.
|
---|
467 | */
|
---|
468 | bool fDaemonize = true;
|
---|
469 | bool fDaemonized = false;
|
---|
470 | for (int i = 1; i < argc; i++)
|
---|
471 | {
|
---|
472 | const char *psz = argv[i];
|
---|
473 | if (*psz != '-')
|
---|
474 | return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
|
---|
475 | psz++;
|
---|
476 |
|
---|
477 | /* translate long argument to short */
|
---|
478 | if (*psz == '-')
|
---|
479 | {
|
---|
480 | psz++;
|
---|
481 | size_t cch = strlen(psz);
|
---|
482 | #define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
|
---|
483 | && !memcmp(psz, strconst, sizeof(strconst) - 1) )
|
---|
484 | if (MATCHES("foreground"))
|
---|
485 | psz = "f";
|
---|
486 | else if (MATCHES("verbose"))
|
---|
487 | psz = "v";
|
---|
488 | else if (MATCHES("help"))
|
---|
489 | psz = "h";
|
---|
490 | else if (MATCHES("interval"))
|
---|
491 | psz = "i";
|
---|
492 | #ifdef RT_OS_WINDOWS
|
---|
493 | else if (MATCHES("register"))
|
---|
494 | psz = "r";
|
---|
495 | else if (MATCHES("unregister"))
|
---|
496 | psz = "u";
|
---|
497 | #endif
|
---|
498 | else if (MATCHES("daemonized"))
|
---|
499 | {
|
---|
500 | fDaemonized = true;
|
---|
501 | continue;
|
---|
502 | }
|
---|
503 | else
|
---|
504 | {
|
---|
505 | bool fFound = false;
|
---|
506 |
|
---|
507 | if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
|
---|
508 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
509 | if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
510 | g_aServices[j].fEnabled = true;
|
---|
511 |
|
---|
512 | if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
|
---|
513 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
514 | if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
515 | g_aServices[j].fEnabled = false;
|
---|
516 |
|
---|
517 | if (!fFound)
|
---|
518 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
519 | {
|
---|
520 | rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
|
---|
521 | fFound = rc == 0;
|
---|
522 | if (fFound)
|
---|
523 | break;
|
---|
524 | if (rc != -1)
|
---|
525 | return rc;
|
---|
526 | }
|
---|
527 | if (!fFound)
|
---|
528 | return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
|
---|
529 | continue;
|
---|
530 | }
|
---|
531 | #undef MATCHES
|
---|
532 | }
|
---|
533 |
|
---|
534 | /* handle the string of short options. */
|
---|
535 | do
|
---|
536 | {
|
---|
537 | switch (*psz)
|
---|
538 | {
|
---|
539 | case 'i':
|
---|
540 | rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
|
---|
541 | &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
|
---|
542 | if (rc)
|
---|
543 | return rc;
|
---|
544 | psz = NULL;
|
---|
545 | break;
|
---|
546 |
|
---|
547 | case 'f':
|
---|
548 | fDaemonize = false;
|
---|
549 | break;
|
---|
550 |
|
---|
551 | case 'v':
|
---|
552 | g_cVerbosity++;
|
---|
553 | break;
|
---|
554 |
|
---|
555 | case 'h':
|
---|
556 | case '?':
|
---|
557 | return VBoxServiceUsage();
|
---|
558 |
|
---|
559 | #ifdef RT_OS_WINDOWS
|
---|
560 | case 'r':
|
---|
561 | return VBoxServiceWinInstall();
|
---|
562 |
|
---|
563 | case 'u':
|
---|
564 | return VBoxServiceWinUninstall();
|
---|
565 | #endif
|
---|
566 |
|
---|
567 | default:
|
---|
568 | {
|
---|
569 | bool fFound = false;
|
---|
570 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
571 | {
|
---|
572 | rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
|
---|
573 | fFound = rc == 0;
|
---|
574 | if (fFound)
|
---|
575 | break;
|
---|
576 | if (rc != -1)
|
---|
577 | return rc;
|
---|
578 | }
|
---|
579 | if (!fFound)
|
---|
580 | return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
|
---|
581 | break;
|
---|
582 | }
|
---|
583 | }
|
---|
584 | } while (psz && *++psz);
|
---|
585 | }
|
---|
586 | /*
|
---|
587 | * Check that at least one service is enabled.
|
---|
588 | */
|
---|
589 | unsigned iMain = VBoxServiceGetStartedServices();
|
---|
590 | if (iMain == ~0U)
|
---|
591 | return VBoxServiceSyntax("At least one service must be enabled.\n");
|
---|
592 | #ifndef RT_OS_WINDOWS
|
---|
593 | /*
|
---|
594 | * POSIX: No main service thread.
|
---|
595 | */
|
---|
596 | iMain = ~0U;
|
---|
597 | #endif
|
---|
598 |
|
---|
599 | VBoxServiceVerbose(0, "%s r%s started. Verbose level = %d\n",
|
---|
600 | RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
|
---|
601 |
|
---|
602 | /*
|
---|
603 | * Daemonize if requested.
|
---|
604 | */
|
---|
605 | if (fDaemonize && !fDaemonized)
|
---|
606 | {
|
---|
607 | #ifdef RT_OS_WINDOWS
|
---|
608 | /** @todo Should do something like VBoxSVC here, OR automatically re-register
|
---|
609 | * the service and start it. Involving VbglR3Daemonize isn't an option
|
---|
610 | * here.
|
---|
611 | *
|
---|
612 | * Also, the idea here, IIRC, was to map the sub service to windows
|
---|
613 | * services. The todo below is for mimicking windows services on
|
---|
614 | * non-windows systems. Not sure if this is doable or not, but in anycase
|
---|
615 | * this code can be moved into -win.
|
---|
616 | *
|
---|
617 | * You should return when StartServiceCtrlDispatcher, btw., not
|
---|
618 | * continue.
|
---|
619 | */
|
---|
620 | VBoxServiceVerbose(2, "Starting service dispatcher ...\n");
|
---|
621 | if (!StartServiceCtrlDispatcher(&g_aServiceTable[0]))
|
---|
622 | return VBoxServiceError("StartServiceCtrlDispatcher: %u. Please start %s with option -f (foreground)!",
|
---|
623 | GetLastError(), g_pszProgName);
|
---|
624 | /* Service now lives in the control dispatcher registered above. */
|
---|
625 | #else
|
---|
626 | VBoxServiceVerbose(1, "Daemonizing...\n");
|
---|
627 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
|
---|
628 | if (RT_FAILURE(rc))
|
---|
629 | return VBoxServiceError("Daemon failed: %Rrc\n", rc);
|
---|
630 | /* in-child */
|
---|
631 | #endif
|
---|
632 | }
|
---|
633 | #ifdef RT_OS_WINDOWS
|
---|
634 | else
|
---|
635 | {
|
---|
636 | /* Run the app just like a console one if not daemonized. */
|
---|
637 | #endif
|
---|
638 | /*
|
---|
639 | * Windows: Start the services, enter the main threads' run loop and stop them
|
---|
640 | * again when it returns.
|
---|
641 | *
|
---|
642 | * POSIX: Start all services and return immediately.
|
---|
643 | */
|
---|
644 | rc = VBoxServiceStartServices(iMain);
|
---|
645 | #ifndef RT_OS_WINDOWS
|
---|
646 | if (RT_SUCCESS(rc))
|
---|
647 | VBoxServiceWaitSignal();
|
---|
648 | #endif
|
---|
649 | VBoxServiceStopServices();
|
---|
650 | #ifdef RT_OS_WINDOWS
|
---|
651 | }
|
---|
652 | #endif
|
---|
653 | #ifdef RT_OS_WINDOWS
|
---|
654 | /*
|
---|
655 | * Release instance mutex if we got it.
|
---|
656 | */
|
---|
657 | if (hMutexAppRunning != NULL)
|
---|
658 | {
|
---|
659 | ::CloseHandle(hMutexAppRunning);
|
---|
660 | hMutexAppRunning = NULL;
|
---|
661 | }
|
---|
662 | #endif
|
---|
663 |
|
---|
664 | VBoxServiceVerbose(0, "Ended.\n");
|
---|
665 | return RT_SUCCESS(rc) ? 0 : 1;
|
---|
666 | }
|
---|
667 |
|
---|