1 | /** $Id: VBoxService.cpp 3655 2007-07-16 18:47:26Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Service Skeleton.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007 innotek GmbH
|
---|
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 as published by the Free Software Foundation,
|
---|
13 | * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
|
---|
14 | * distribution. VirtualBox OSE is distributed in the hope that it will
|
---|
15 | * be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | *
|
---|
17 | * If you received this file as part of a commercial VirtualBox
|
---|
18 | * distribution, then only the terms of your commercial VirtualBox
|
---|
19 | * license agreement apply instead of the previous paragraph.
|
---|
20 | */
|
---|
21 |
|
---|
22 |
|
---|
23 |
|
---|
24 | /*******************************************************************************
|
---|
25 | * Header Files *
|
---|
26 | *******************************************************************************/
|
---|
27 | #include <unistd.h>
|
---|
28 | #include <errno.h>
|
---|
29 |
|
---|
30 | #include <iprt/thread.h>
|
---|
31 | #include <iprt/string.h>
|
---|
32 | #include <iprt/stream.h>
|
---|
33 | #include <iprt/initterm.h>
|
---|
34 | #include <iprt/asm.h>
|
---|
35 | #include <iprt/path.h>
|
---|
36 | #include <VBox/VBoxGuest.h>
|
---|
37 | #include "VBoxServiceInternal.h"
|
---|
38 |
|
---|
39 |
|
---|
40 | /*******************************************************************************
|
---|
41 | * Global Variables *
|
---|
42 | *******************************************************************************/
|
---|
43 | /** The program name (derived from argv[0]). */
|
---|
44 | char *g_pszProgName = "";
|
---|
45 | /** The current verbosity level. */
|
---|
46 | int g_cVerbosity = 0;
|
---|
47 | /** The default service interval (the -i | --interval) option). */
|
---|
48 | uint32_t g_DefaultInterval = 0;
|
---|
49 | /** Shutdown the main thread. (later, for signals) */
|
---|
50 | bool volatile g_fShutdown;
|
---|
51 |
|
---|
52 | /**
|
---|
53 | * The details of the services that has been compiled in.
|
---|
54 | */
|
---|
55 | static struct
|
---|
56 | {
|
---|
57 | /** Pointer to the service descriptor. */
|
---|
58 | PCVBOXSERVICE pDesc;
|
---|
59 | /** The worker thread. NIL_RTTHREAD if it's the main thread. */
|
---|
60 | RTTHREAD Thread;
|
---|
61 | /** Shutdown indicator. */
|
---|
62 | bool volatile fShutdown;
|
---|
63 | /** Indicator set by the service thread exiting. */
|
---|
64 | bool volatile fStopped;
|
---|
65 | /** Whether the service was started or not. */
|
---|
66 | bool fStarted;
|
---|
67 | /** Whether the service is enabled or not. */
|
---|
68 | bool fEnabled;
|
---|
69 | } g_aServices[] =
|
---|
70 | {
|
---|
71 | #ifdef VBOXSERVICE_CONTROL
|
---|
72 | { &g_Control, NIL_RTTHREAD, false, false, false, true },
|
---|
73 | #endif
|
---|
74 | #ifdef VBOXSERVICE_TIMESYNC
|
---|
75 | { &g_TimeSync, NIL_RTTHREAD, false, false, false, true },
|
---|
76 | #endif
|
---|
77 | #ifdef VBOXSERVICE_CLIPBOARD
|
---|
78 | { &g_Clipboard, NIL_RTTHREAD, false, false, false, true },
|
---|
79 | #endif
|
---|
80 | };
|
---|
81 |
|
---|
82 |
|
---|
83 | /**
|
---|
84 | * Displays the program usage message.
|
---|
85 | *
|
---|
86 | * @returns 1.
|
---|
87 | */
|
---|
88 | static int VBoxServiceUsage(void)
|
---|
89 | {
|
---|
90 | RTPrintf("usage: %s [-f|--foreground] [-v|--verbose] [-i|--interval <seconds>]\n"
|
---|
91 | " [--disable-<service>] [--enable-<service>] [-h|-?|--help]\n", g_pszProgName);
|
---|
92 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
93 | RTPrintf(" %s\n", g_aServices[j].pDesc->pszUsage);
|
---|
94 | RTPrintf("\n"
|
---|
95 | "Options:\n"
|
---|
96 | " -f | --foreground Don't daemonzie the program. For debugging.\n"
|
---|
97 | " -v | --verbose Increment the verbosity level. For debugging.\n"
|
---|
98 | " -i | --interval The default interval.\n"
|
---|
99 | " -h | -? | --help Show this message and exit with status 1.\n");
|
---|
100 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
101 | {
|
---|
102 | RTPrintf(" --enable-%-10s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
103 | RTPrintf(" --disable-%-9s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
104 | RTPrintf("%s", g_aServices[j].pDesc->pszOptions);
|
---|
105 | }
|
---|
106 | RTPrintf("\n"
|
---|
107 | " Copyright (C) 2007 innotek GmbH\n");
|
---|
108 |
|
---|
109 | return 1;
|
---|
110 | }
|
---|
111 |
|
---|
112 |
|
---|
113 | /**
|
---|
114 | * Displays a syntax error message.
|
---|
115 | *
|
---|
116 | * @returns 1
|
---|
117 | * @param pszFormat The message text.
|
---|
118 | * @param ... Format arguments.
|
---|
119 | */
|
---|
120 | int VBoxServiceSyntax(const char *pszFormat, ...)
|
---|
121 | {
|
---|
122 | RTStrmPrintf(g_pStdErr, "%s: syntax error: ", g_pszProgName);
|
---|
123 |
|
---|
124 | va_list va;
|
---|
125 | va_start(va, pszFormat);
|
---|
126 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
127 | va_end(va);
|
---|
128 |
|
---|
129 | return 1;
|
---|
130 | }
|
---|
131 |
|
---|
132 |
|
---|
133 | /**
|
---|
134 | * Displays an error message.
|
---|
135 | *
|
---|
136 | * @returns 1
|
---|
137 | * @param pszFormat The message text.
|
---|
138 | * @param ... Format arguments.
|
---|
139 | */
|
---|
140 | int VBoxServiceError(const char *pszFormat, ...)
|
---|
141 | {
|
---|
142 | RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
|
---|
143 |
|
---|
144 | va_list va;
|
---|
145 | va_start(va, pszFormat);
|
---|
146 | RTStrmPrintfV(g_pStdErr, pszFormat, va);
|
---|
147 | va_end(va);
|
---|
148 |
|
---|
149 | return 1;
|
---|
150 | }
|
---|
151 |
|
---|
152 |
|
---|
153 | /**
|
---|
154 | * Displays a verbose message.
|
---|
155 | *
|
---|
156 | * @returns 1
|
---|
157 | * @param pszFormat The message text.
|
---|
158 | * @param ... Format arguments.
|
---|
159 | */
|
---|
160 | void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...)
|
---|
161 | {
|
---|
162 | if (iLevel <= g_cVerbosity)
|
---|
163 | {
|
---|
164 | RTStrmPrintf(g_pStdOut, "%s: ", g_pszProgName);
|
---|
165 | va_list va;
|
---|
166 | va_start(va, pszFormat);
|
---|
167 | RTStrmPrintfV(g_pStdOut, pszFormat, va);
|
---|
168 | va_end(va);
|
---|
169 | }
|
---|
170 | }
|
---|
171 |
|
---|
172 |
|
---|
173 | /**
|
---|
174 | * Gets a 32-bit value argument.
|
---|
175 | *
|
---|
176 | * @returns 0 on success, non-zero exit code on error.
|
---|
177 | * @param argc The argument count.
|
---|
178 | * @param argv The argument vector
|
---|
179 | * @param psz Where in *pi to start looking for the value argument.
|
---|
180 | * @param pi Where to find and perhaps update the argument index.
|
---|
181 | * @param pu32 Where to store the 32-bit value.
|
---|
182 | * @param u32Min The minimum value.
|
---|
183 | * @param u32Max The maximum value.
|
---|
184 | */
|
---|
185 | int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
|
---|
186 | {
|
---|
187 | if (*psz == ':' || *psz == '=')
|
---|
188 | psz++;
|
---|
189 | if (!*psz)
|
---|
190 | {
|
---|
191 | if (*pi + 1 >= argc)
|
---|
192 | return VBoxServiceSyntax("Missing value for the '%s' argument\n", argv[*pi]);
|
---|
193 | psz = argv[++*pi];
|
---|
194 | }
|
---|
195 |
|
---|
196 | char *pszNext;
|
---|
197 | int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
|
---|
198 | if (RT_FAILURE(rc) || *pszNext)
|
---|
199 | return VBoxServiceSyntax("Failed to convert interval '%s' to a number.\n", psz);
|
---|
200 | if (*pu32 < u32Min || *pu32 > u32Max)
|
---|
201 | return VBoxServiceSyntax("The timesync interval of %RU32 secconds is out of range [%RU32..%RU32].\n",
|
---|
202 | *pu32, u32Min, u32Max);
|
---|
203 | return 0;
|
---|
204 | }
|
---|
205 |
|
---|
206 |
|
---|
207 | /**
|
---|
208 | * The service thread.
|
---|
209 | *
|
---|
210 | * @returns Whatever the worker function returns.
|
---|
211 | * @param ThreadSelf My thread handle.
|
---|
212 | * @param pvUser The service index.
|
---|
213 | */
|
---|
214 | static DECLCALLBACK(int) VBoxServiceThread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
215 | {
|
---|
216 | const unsigned i = (uintptr_t)pvUser;
|
---|
217 | int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
|
---|
218 | ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
|
---|
219 | RTThreadUserSignal(ThreadSelf);
|
---|
220 | return rc;
|
---|
221 | }
|
---|
222 |
|
---|
223 | int main(int argc, char **argv)
|
---|
224 | {
|
---|
225 | int rc;
|
---|
226 |
|
---|
227 | /*
|
---|
228 | * Init globals and such.
|
---|
229 | */
|
---|
230 | RTR3Init(false, 0);
|
---|
231 | g_pszProgName = RTPathFilename(argv[0]);
|
---|
232 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
233 | {
|
---|
234 | rc = g_aServices[j].pDesc->pfnPreInit();
|
---|
235 | if (RT_FAILURE(rc))
|
---|
236 | return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
|
---|
237 | }
|
---|
238 |
|
---|
239 | /*
|
---|
240 | * Parse the arguments.
|
---|
241 | */
|
---|
242 | bool fDaemonize = true;
|
---|
243 | bool fDaemonzied = false;
|
---|
244 | for (int i = 1; i < argc; i++)
|
---|
245 | {
|
---|
246 | const char *psz = argv[i];
|
---|
247 | if (*psz != '-')
|
---|
248 | return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
|
---|
249 | psz++;
|
---|
250 |
|
---|
251 | /* translate long argument to short */
|
---|
252 | if (*psz == '-')
|
---|
253 | {
|
---|
254 | psz++;
|
---|
255 | size_t cch = strlen(psz);
|
---|
256 | #define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
|
---|
257 | && !memcmp(psz, strconst, sizeof(strconst) - 1) )
|
---|
258 | if (MATCHES("foreground"))
|
---|
259 | psz = "f";
|
---|
260 | else if (MATCHES("verbose"))
|
---|
261 | psz = "v";
|
---|
262 | else if (MATCHES("help"))
|
---|
263 | psz = "h";
|
---|
264 | else if (MATCHES("interval"))
|
---|
265 | psz = "i";
|
---|
266 | else if (MATCHES("daemonized"))
|
---|
267 | {
|
---|
268 | fDaemonzied = true;
|
---|
269 | continue;
|
---|
270 | }
|
---|
271 | else
|
---|
272 | {
|
---|
273 | bool fFound = false;
|
---|
274 |
|
---|
275 | if (cch > sizeof("enable-") && !memcmp(psz, "enable-", sizeof("enable-") - 1))
|
---|
276 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
277 | if ((fFound = !stricmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
278 | g_aServices[j].fEnabled = true;
|
---|
279 |
|
---|
280 | if (cch > sizeof("disable-") && !memcmp(psz, "disable-", sizeof("disable-") - 1))
|
---|
281 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
282 | if ((fFound = !stricmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
283 | g_aServices[j].fEnabled = false;
|
---|
284 |
|
---|
285 | if (!fFound)
|
---|
286 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
287 | {
|
---|
288 | rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
|
---|
289 | fFound = rc == 0;
|
---|
290 | if (fFound)
|
---|
291 | break;
|
---|
292 | if (rc != -1)
|
---|
293 | return rc;
|
---|
294 | }
|
---|
295 | if (!fFound)
|
---|
296 | return VBoxServiceSyntax("Unknown option '%s'\n", argv[i]);
|
---|
297 | continue;
|
---|
298 | }
|
---|
299 | #undef MATCHES
|
---|
300 | }
|
---|
301 |
|
---|
302 | /* handle the string of short options. */
|
---|
303 | do
|
---|
304 | {
|
---|
305 | switch (*psz)
|
---|
306 | {
|
---|
307 | case 'i':
|
---|
308 | rc = VBoxServiceArgUInt32(argc, argv, psz + 1, &i,
|
---|
309 | &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
|
---|
310 | if (rc)
|
---|
311 | return rc;
|
---|
312 | psz = NULL;
|
---|
313 | break;
|
---|
314 |
|
---|
315 | case 'f':
|
---|
316 | fDaemonize = false;
|
---|
317 | break;
|
---|
318 |
|
---|
319 | case 'v':
|
---|
320 | g_cVerbosity++;
|
---|
321 | break;
|
---|
322 |
|
---|
323 | case 'h':
|
---|
324 | return VBoxServiceUsage();
|
---|
325 |
|
---|
326 | default:
|
---|
327 | {
|
---|
328 | bool fFound = false;
|
---|
329 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
330 | {
|
---|
331 | rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
|
---|
332 | fFound = rc == 0;
|
---|
333 | if (fFound)
|
---|
334 | break;
|
---|
335 | if (rc != -1)
|
---|
336 | return rc;
|
---|
337 | }
|
---|
338 | if (!fFound)
|
---|
339 | return VBoxServiceSyntax("Unknown option '%c' (%s)\n", *psz, argv[i]);
|
---|
340 | break;
|
---|
341 | }
|
---|
342 | }
|
---|
343 | } while (psz && *++psz);
|
---|
344 | }
|
---|
345 |
|
---|
346 | /*
|
---|
347 | * Check that at least one service is enabled.
|
---|
348 | */
|
---|
349 | unsigned iMain = ~0U;
|
---|
350 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
351 | if (g_aServices[j].fEnabled)
|
---|
352 | {
|
---|
353 | iMain = j;
|
---|
354 | break;
|
---|
355 | }
|
---|
356 | if (iMain == ~0U)
|
---|
357 | return VBoxServiceSyntax("At least one service must be enabled.\n");
|
---|
358 |
|
---|
359 | /*
|
---|
360 | * Connect to the kernel part before daemonizing so we can fail
|
---|
361 | * and complain if there is some kind of problem.
|
---|
362 | */
|
---|
363 | VBoxServiceVerbose(2, "Calling VbgR3Init()\n");
|
---|
364 | rc = VbglR3Init();
|
---|
365 | if (RT_FAILURE(rc))
|
---|
366 | return VBoxServiceError("VbglR3Init failed with rc=%Rrc.\n", rc);
|
---|
367 |
|
---|
368 | /*
|
---|
369 | * Daemonize if requested.
|
---|
370 | */
|
---|
371 | if (fDaemonize && !fDaemonzied)
|
---|
372 | {
|
---|
373 | VBoxServiceVerbose(1, "Daemonizing...\n");
|
---|
374 | errno = 0;
|
---|
375 | if (daemon(0, 0) != 0)
|
---|
376 | return VBoxServiceError("daemon failed: %s\n", strerror(errno));
|
---|
377 | /* in-child */
|
---|
378 | }
|
---|
379 |
|
---|
380 | /** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
|
---|
381 |
|
---|
382 | /*
|
---|
383 | * Initialize the services.
|
---|
384 | */
|
---|
385 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
386 | {
|
---|
387 | rc = g_aServices[j].pDesc->pfnInit();
|
---|
388 | if (RT_FAILURE(rc))
|
---|
389 | return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
|
---|
390 | }
|
---|
391 |
|
---|
392 | /*
|
---|
393 | * Start the service(s).
|
---|
394 | */
|
---|
395 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
396 | {
|
---|
397 | if ( !g_aServices[j].fEnabled
|
---|
398 | || j == iMain)
|
---|
399 | continue;
|
---|
400 |
|
---|
401 | rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
|
---|
402 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
|
---|
403 | if (RT_FAILURE(rc))
|
---|
404 | {
|
---|
405 | VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
|
---|
406 | break;
|
---|
407 | }
|
---|
408 | g_aServices[j].fStarted = true;
|
---|
409 |
|
---|
410 | /* wait for the thread to initialize */
|
---|
411 | RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
|
---|
412 | if (g_aServices[j].fShutdown)
|
---|
413 | rc = VERR_GENERAL_FAILURE;
|
---|
414 | }
|
---|
415 | if (RT_SUCCESS(rc))
|
---|
416 | {
|
---|
417 | /* The final service runs in the main thread. */
|
---|
418 | VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
|
---|
419 | rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
|
---|
420 | VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
|
---|
421 | }
|
---|
422 |
|
---|
423 | /*
|
---|
424 | * Stop and terminate the services.
|
---|
425 | */
|
---|
426 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
427 | ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
|
---|
428 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
429 | if (g_aServices[j].fStarted)
|
---|
430 | g_aServices[j].pDesc->pfnStop();
|
---|
431 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
432 | {
|
---|
433 | if (g_aServices[j].Thread != NIL_RTTHREAD)
|
---|
434 | {
|
---|
435 | rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
|
---|
436 | if (RT_FAILURE(rc))
|
---|
437 | VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
|
---|
438 | }
|
---|
439 | g_aServices[j].pDesc->pfnTerm();
|
---|
440 | }
|
---|
441 |
|
---|
442 | return 0;
|
---|
443 | }
|
---|
444 |
|
---|