1 | /* $Id: VBoxService.cpp 83409 2020-03-25 13:15:39Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VBoxService - Guest Additions Service Skeleton.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2007-2020 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 | /** @page pg_vgsvc VBoxService
|
---|
20 | *
|
---|
21 | * VBoxService is a root daemon for implementing guest additions features.
|
---|
22 | *
|
---|
23 | * It is structured as one binary that contains many sub-services. The reason
|
---|
24 | * for this is partially historical and partially practical. The practical
|
---|
25 | * reason is that the VBoxService binary is typically statically linked, at
|
---|
26 | * least with IPRT and the guest library, so we save quite a lot of space having
|
---|
27 | * on single binary instead individual binaries for each sub-service and their
|
---|
28 | * helpers (currently up to 9 subservices and 8 helpers). The historical is
|
---|
29 | * simply that it started its life on OS/2 dreaming of conquring Windows next,
|
---|
30 | * so it kind of felt natural to have it all in one binary.
|
---|
31 | *
|
---|
32 | * Even if it's structured as a single binary, it is possible, by using command
|
---|
33 | * line options, to start each subservice as an individual process.
|
---|
34 | *
|
---|
35 | * Subservices:
|
---|
36 | * - @subpage pg_vgsvc_timesync "Time Synchronization"
|
---|
37 | * - @subpage pg_vgsvc_vminfo "VM Information"
|
---|
38 | * - @subpage pg_vgsvc_vmstats "VM Statistics"
|
---|
39 | * - @subpage pg_vgsvc_gstctrl "Guest Control"
|
---|
40 | * - @subpage pg_vgsvc_pagesharing "Page Sharing"
|
---|
41 | * - @subpage pg_vgsvc_memballoon "Memory Balooning"
|
---|
42 | * - @subpage pg_vgsvc_cpuhotplug "CPU Hot-Plugging"
|
---|
43 | * - @subpage pg_vgsvc_automount "Shared Folder Automounting"
|
---|
44 | * - @subpage pg_vgsvc_clipboard "Clipboard (OS/2 only)"
|
---|
45 | *
|
---|
46 | * Now, since the service predates a lot of stuff, including RTGetOpt, we're
|
---|
47 | * currently doing our own version of argument parsing here, which is kind of
|
---|
48 | * stupid. That will hopefully be cleaned up eventually.
|
---|
49 | */
|
---|
50 |
|
---|
51 |
|
---|
52 | /*********************************************************************************************************************************
|
---|
53 | * Header Files *
|
---|
54 | *********************************************************************************************************************************/
|
---|
55 | /** @todo LOG_GROUP*/
|
---|
56 | #ifndef _MSC_VER
|
---|
57 | # include <unistd.h>
|
---|
58 | #endif
|
---|
59 | #include <errno.h>
|
---|
60 | #ifndef RT_OS_WINDOWS
|
---|
61 | # include <signal.h>
|
---|
62 | # ifdef RT_OS_OS2
|
---|
63 | # define pthread_sigmask sigprocmask
|
---|
64 | # endif
|
---|
65 | #endif
|
---|
66 | #ifdef RT_OS_FREEBSD
|
---|
67 | # include <pthread.h>
|
---|
68 | #endif
|
---|
69 |
|
---|
70 | #include <package-generated.h>
|
---|
71 | #include "product-generated.h"
|
---|
72 |
|
---|
73 | #include <iprt/asm.h>
|
---|
74 | #include <iprt/buildconfig.h>
|
---|
75 | #include <iprt/initterm.h>
|
---|
76 | #include <iprt/file.h>
|
---|
77 | #ifdef DEBUG
|
---|
78 | # include <iprt/memtracker.h>
|
---|
79 | #endif
|
---|
80 | #include <iprt/message.h>
|
---|
81 | #include <iprt/path.h>
|
---|
82 | #include <iprt/process.h>
|
---|
83 | #include <iprt/semaphore.h>
|
---|
84 | #include <iprt/string.h>
|
---|
85 | #include <iprt/stream.h>
|
---|
86 | #include <iprt/system.h>
|
---|
87 | #include <iprt/thread.h>
|
---|
88 |
|
---|
89 | #include <VBox/err.h>
|
---|
90 | #include <VBox/log.h>
|
---|
91 |
|
---|
92 | #include "VBoxServiceInternal.h"
|
---|
93 | #ifdef VBOX_WITH_VBOXSERVICE_CONTROL
|
---|
94 | # include "VBoxServiceControl.h"
|
---|
95 | #endif
|
---|
96 | #ifdef VBOX_WITH_VBOXSERVICE_TOOLBOX
|
---|
97 | # include "VBoxServiceToolBox.h"
|
---|
98 | #endif
|
---|
99 |
|
---|
100 |
|
---|
101 | /*********************************************************************************************************************************
|
---|
102 | * Global Variables *
|
---|
103 | *********************************************************************************************************************************/
|
---|
104 | /** The program name (derived from argv[0]). */
|
---|
105 | char *g_pszProgName = (char *)"";
|
---|
106 | /** The current verbosity level. */
|
---|
107 | unsigned g_cVerbosity = 0;
|
---|
108 | char g_szLogFile[RTPATH_MAX + 128] = "";
|
---|
109 | char g_szPidFile[RTPATH_MAX] = "";
|
---|
110 | /** Logging parameters. */
|
---|
111 | /** @todo Make this configurable later. */
|
---|
112 | static PRTLOGGER g_pLoggerRelease = NULL;
|
---|
113 | static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */
|
---|
114 | static uint32_t g_uHistoryFileTime = RT_SEC_1DAY; /* Max 1 day per file. */
|
---|
115 | static uint64_t g_uHistoryFileSize = 100 * _1M; /* Max 100MB per file. */
|
---|
116 | /** Critical section for (debug) logging. */
|
---|
117 | #ifdef DEBUG
|
---|
118 | RTCRITSECT g_csLog;
|
---|
119 | #endif
|
---|
120 | /** The default service interval (the -i | --interval) option). */
|
---|
121 | uint32_t g_DefaultInterval = 0;
|
---|
122 | #ifdef RT_OS_WINDOWS
|
---|
123 | /** Signal shutdown to the Windows service thread. */
|
---|
124 | static bool volatile g_fWindowsServiceShutdown;
|
---|
125 | /** Event the Windows service thread waits for shutdown. */
|
---|
126 | static RTSEMEVENT g_hEvtWindowsService;
|
---|
127 | #endif
|
---|
128 |
|
---|
129 | /**
|
---|
130 | * The details of the services that has been compiled in.
|
---|
131 | */
|
---|
132 | static struct
|
---|
133 | {
|
---|
134 | /** Pointer to the service descriptor. */
|
---|
135 | PCVBOXSERVICE pDesc;
|
---|
136 | /** The worker thread. NIL_RTTHREAD if it's the main thread. */
|
---|
137 | RTTHREAD Thread;
|
---|
138 | /** Whether Pre-init was called. */
|
---|
139 | bool fPreInited;
|
---|
140 | /** Shutdown indicator. */
|
---|
141 | bool volatile fShutdown;
|
---|
142 | /** Indicator set by the service thread exiting. */
|
---|
143 | bool volatile fStopped;
|
---|
144 | /** Whether the service was started or not. */
|
---|
145 | bool fStarted;
|
---|
146 | /** Whether the service is enabled or not. */
|
---|
147 | bool fEnabled;
|
---|
148 | } g_aServices[] =
|
---|
149 | {
|
---|
150 | #ifdef VBOX_WITH_VBOXSERVICE_CONTROL
|
---|
151 | { &g_Control, NIL_RTTHREAD, false, false, false, false, true },
|
---|
152 | #endif
|
---|
153 | #ifdef VBOX_WITH_VBOXSERVICE_TIMESYNC
|
---|
154 | { &g_TimeSync, NIL_RTTHREAD, false, false, false, false, true },
|
---|
155 | #endif
|
---|
156 | #ifdef VBOX_WITH_VBOXSERVICE_CLIPBOARD
|
---|
157 | { &g_Clipboard, NIL_RTTHREAD, false, false, false, false, true },
|
---|
158 | #endif
|
---|
159 | #ifdef VBOX_WITH_VBOXSERVICE_VMINFO
|
---|
160 | { &g_VMInfo, NIL_RTTHREAD, false, false, false, false, true },
|
---|
161 | #endif
|
---|
162 | #ifdef VBOX_WITH_VBOXSERVICE_CPUHOTPLUG
|
---|
163 | { &g_CpuHotPlug, NIL_RTTHREAD, false, false, false, false, true },
|
---|
164 | #endif
|
---|
165 | #ifdef VBOX_WITH_VBOXSERVICE_MANAGEMENT
|
---|
166 | # ifdef VBOX_WITH_MEMBALLOON
|
---|
167 | { &g_MemBalloon, NIL_RTTHREAD, false, false, false, false, true },
|
---|
168 | # endif
|
---|
169 | { &g_VMStatistics, NIL_RTTHREAD, false, false, false, false, true },
|
---|
170 | #endif
|
---|
171 | #if defined(VBOX_WITH_VBOXSERVICE_PAGE_SHARING)
|
---|
172 | { &g_PageSharing, NIL_RTTHREAD, false, false, false, false, true },
|
---|
173 | #endif
|
---|
174 | #ifdef VBOX_WITH_SHARED_FOLDERS
|
---|
175 | { &g_AutoMount, NIL_RTTHREAD, false, false, false, false, true },
|
---|
176 | #endif
|
---|
177 | };
|
---|
178 |
|
---|
179 |
|
---|
180 | /*
|
---|
181 | * Default call-backs for services which do not need special behaviour.
|
---|
182 | */
|
---|
183 |
|
---|
184 | /**
|
---|
185 | * @interface_method_impl{VBOXSERVICE,pfnPreInit, Default Implementation}
|
---|
186 | */
|
---|
187 | DECLCALLBACK(int) VGSvcDefaultPreInit(void)
|
---|
188 | {
|
---|
189 | return VINF_SUCCESS;
|
---|
190 | }
|
---|
191 |
|
---|
192 |
|
---|
193 | /**
|
---|
194 | * @interface_method_impl{VBOXSERVICE,pfnOption, Default Implementation}
|
---|
195 | */
|
---|
196 | DECLCALLBACK(int) VGSvcDefaultOption(const char **ppszShort, int argc,
|
---|
197 | char **argv, int *pi)
|
---|
198 | {
|
---|
199 | NOREF(ppszShort);
|
---|
200 | NOREF(argc);
|
---|
201 | NOREF(argv);
|
---|
202 | NOREF(pi);
|
---|
203 |
|
---|
204 | return -1;
|
---|
205 | }
|
---|
206 |
|
---|
207 |
|
---|
208 | /**
|
---|
209 | * @interface_method_impl{VBOXSERVICE,pfnInit, Default Implementation}
|
---|
210 | */
|
---|
211 | DECLCALLBACK(int) VGSvcDefaultInit(void)
|
---|
212 | {
|
---|
213 | return VINF_SUCCESS;
|
---|
214 | }
|
---|
215 |
|
---|
216 |
|
---|
217 | /**
|
---|
218 | * @interface_method_impl{VBOXSERVICE,pfnTerm, Default Implementation}
|
---|
219 | */
|
---|
220 | DECLCALLBACK(void) VGSvcDefaultTerm(void)
|
---|
221 | {
|
---|
222 | return;
|
---|
223 | }
|
---|
224 |
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * @callback_method_impl{FNRTLOGPHASE, Release logger callback}
|
---|
228 | */
|
---|
229 | static DECLCALLBACK(void) vgsvcLogHeaderFooter(PRTLOGGER pLoggerRelease, RTLOGPHASE enmPhase, PFNRTLOGPHASEMSG pfnLog)
|
---|
230 | {
|
---|
231 | /* Some introductory information. */
|
---|
232 | static RTTIMESPEC s_TimeSpec;
|
---|
233 | char szTmp[256];
|
---|
234 | if (enmPhase == RTLOGPHASE_BEGIN)
|
---|
235 | RTTimeNow(&s_TimeSpec);
|
---|
236 | RTTimeSpecToString(&s_TimeSpec, szTmp, sizeof(szTmp));
|
---|
237 |
|
---|
238 | switch (enmPhase)
|
---|
239 | {
|
---|
240 | case RTLOGPHASE_BEGIN:
|
---|
241 | {
|
---|
242 | pfnLog(pLoggerRelease,
|
---|
243 | "VBoxService %s r%s (verbosity: %u) %s (%s %s) release log\n"
|
---|
244 | "Log opened %s\n",
|
---|
245 | RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity, VBOX_BUILD_TARGET,
|
---|
246 | __DATE__, __TIME__, szTmp);
|
---|
247 |
|
---|
248 | int vrc = RTSystemQueryOSInfo(RTSYSOSINFO_PRODUCT, szTmp, sizeof(szTmp));
|
---|
249 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
250 | pfnLog(pLoggerRelease, "OS Product: %s\n", szTmp);
|
---|
251 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_RELEASE, szTmp, sizeof(szTmp));
|
---|
252 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
253 | pfnLog(pLoggerRelease, "OS Release: %s\n", szTmp);
|
---|
254 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_VERSION, szTmp, sizeof(szTmp));
|
---|
255 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
256 | pfnLog(pLoggerRelease, "OS Version: %s\n", szTmp);
|
---|
257 | vrc = RTSystemQueryOSInfo(RTSYSOSINFO_SERVICE_PACK, szTmp, sizeof(szTmp));
|
---|
258 | if (RT_SUCCESS(vrc) || vrc == VERR_BUFFER_OVERFLOW)
|
---|
259 | pfnLog(pLoggerRelease, "OS Service Pack: %s\n", szTmp);
|
---|
260 |
|
---|
261 | /* the package type is interesting for Linux distributions */
|
---|
262 | char szExecName[RTPATH_MAX];
|
---|
263 | char *pszExecName = RTProcGetExecutablePath(szExecName, sizeof(szExecName));
|
---|
264 | pfnLog(pLoggerRelease,
|
---|
265 | "Executable: %s\n"
|
---|
266 | "Process ID: %u\n"
|
---|
267 | "Package type: %s"
|
---|
268 | #ifdef VBOX_OSE
|
---|
269 | " (OSE)"
|
---|
270 | #endif
|
---|
271 | "\n",
|
---|
272 | pszExecName ? pszExecName : "unknown",
|
---|
273 | RTProcSelf(),
|
---|
274 | VBOX_PACKAGE_STRING);
|
---|
275 | break;
|
---|
276 | }
|
---|
277 |
|
---|
278 | case RTLOGPHASE_PREROTATE:
|
---|
279 | pfnLog(pLoggerRelease, "Log rotated - Log started %s\n", szTmp);
|
---|
280 | break;
|
---|
281 |
|
---|
282 | case RTLOGPHASE_POSTROTATE:
|
---|
283 | pfnLog(pLoggerRelease, "Log continuation - Log started %s\n", szTmp);
|
---|
284 | break;
|
---|
285 |
|
---|
286 | case RTLOGPHASE_END:
|
---|
287 | pfnLog(pLoggerRelease, "End of log file - Log started %s\n", szTmp);
|
---|
288 | break;
|
---|
289 |
|
---|
290 | default:
|
---|
291 | /* nothing */
|
---|
292 | break;
|
---|
293 | }
|
---|
294 | }
|
---|
295 |
|
---|
296 |
|
---|
297 | /**
|
---|
298 | * Creates the default release logger outputting to the specified file.
|
---|
299 | *
|
---|
300 | * Pass NULL to disabled logging.
|
---|
301 | *
|
---|
302 | * @return IPRT status code.
|
---|
303 | * @param pszLogFile Filename for log output. NULL disables logging
|
---|
304 | * (r=bird: No, it doesn't!).
|
---|
305 | */
|
---|
306 | int VGSvcLogCreate(const char *pszLogFile)
|
---|
307 | {
|
---|
308 | /* Create release logger (stdout + file). */
|
---|
309 | static const char * const s_apszGroups[] = VBOX_LOGGROUP_NAMES;
|
---|
310 | RTUINT fFlags = RTLOGFLAGS_PREFIX_THREAD | RTLOGFLAGS_PREFIX_TIME;
|
---|
311 | #if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
|
---|
312 | fFlags |= RTLOGFLAGS_USECRLF;
|
---|
313 | #endif
|
---|
314 | int rc = RTLogCreateEx(&g_pLoggerRelease, fFlags, "all",
|
---|
315 | #ifdef DEBUG
|
---|
316 | "VBOXSERVICE_LOG",
|
---|
317 | #else
|
---|
318 | "VBOXSERVICE_RELEASE_LOG",
|
---|
319 | #endif
|
---|
320 | RT_ELEMENTS(s_apszGroups), s_apszGroups, UINT32_MAX /*cMaxEntriesPerGroup*/,
|
---|
321 | RTLOGDEST_STDOUT | RTLOGDEST_USER,
|
---|
322 | vgsvcLogHeaderFooter, g_cHistory, g_uHistoryFileSize, g_uHistoryFileTime,
|
---|
323 | NULL /*pErrInfo*/, "%s", pszLogFile ? pszLogFile : "");
|
---|
324 | if (RT_SUCCESS(rc))
|
---|
325 | {
|
---|
326 | /* register this logger as the release logger */
|
---|
327 | RTLogRelSetDefaultInstance(g_pLoggerRelease);
|
---|
328 |
|
---|
329 | /* Explicitly flush the log in case of VBOXSERVICE_RELEASE_LOG=buffered. */
|
---|
330 | RTLogFlush(g_pLoggerRelease);
|
---|
331 | }
|
---|
332 |
|
---|
333 | return rc;
|
---|
334 | }
|
---|
335 |
|
---|
336 |
|
---|
337 | /**
|
---|
338 | * Logs a verbose message.
|
---|
339 | *
|
---|
340 | * @param pszFormat The message text.
|
---|
341 | * @param va Format arguments.
|
---|
342 | */
|
---|
343 | void VGSvcLogV(const char *pszFormat, va_list va)
|
---|
344 | {
|
---|
345 | #ifdef DEBUG
|
---|
346 | int rc = RTCritSectEnter(&g_csLog);
|
---|
347 | if (RT_SUCCESS(rc))
|
---|
348 | {
|
---|
349 | #endif
|
---|
350 | char *psz = NULL;
|
---|
351 | RTStrAPrintfV(&psz, pszFormat, va);
|
---|
352 |
|
---|
353 | AssertPtr(psz);
|
---|
354 | LogRel(("%s", psz));
|
---|
355 |
|
---|
356 | RTStrFree(psz);
|
---|
357 | #ifdef DEBUG
|
---|
358 | RTCritSectLeave(&g_csLog);
|
---|
359 | }
|
---|
360 | #endif
|
---|
361 | }
|
---|
362 |
|
---|
363 |
|
---|
364 | /**
|
---|
365 | * Destroys the currently active logging instance.
|
---|
366 | */
|
---|
367 | void VGSvcLogDestroy(void)
|
---|
368 | {
|
---|
369 | RTLogDestroy(RTLogRelSetDefaultInstance(NULL));
|
---|
370 | }
|
---|
371 |
|
---|
372 |
|
---|
373 | /**
|
---|
374 | * Displays the program usage message.
|
---|
375 | *
|
---|
376 | * @returns 1.
|
---|
377 | */
|
---|
378 | static int vgsvcUsage(void)
|
---|
379 | {
|
---|
380 | RTPrintf("Usage:\n"
|
---|
381 | " %-12s [-f|--foreground] [-v|--verbose] [-l|--logfile <file>]\n"
|
---|
382 | " [-p|--pidfile <file>] [-i|--interval <seconds>]\n"
|
---|
383 | " [--disable-<service>] [--enable-<service>]\n"
|
---|
384 | " [--only-<service>] [-h|-?|--help]\n", g_pszProgName);
|
---|
385 | #ifdef RT_OS_WINDOWS
|
---|
386 | RTPrintf(" [-r|--register] [-u|--unregister]\n");
|
---|
387 | #endif
|
---|
388 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
389 | if (g_aServices[j].pDesc->pszUsage)
|
---|
390 | RTPrintf("%s\n", g_aServices[j].pDesc->pszUsage);
|
---|
391 | RTPrintf("\n"
|
---|
392 | "Options:\n"
|
---|
393 | " -i | --interval The default interval.\n"
|
---|
394 | " -f | --foreground Don't daemonize the program. For debugging.\n"
|
---|
395 | " -l | --logfile <file> Enables logging to a file.\n"
|
---|
396 | " -p | --pidfile <file> Write the process ID to a file.\n"
|
---|
397 | " -v | --verbose Increment the verbosity level. For debugging.\n"
|
---|
398 | " -V | --version Show version information.\n"
|
---|
399 | " -h | -? | --help Show this message and exit with status 1.\n"
|
---|
400 | );
|
---|
401 | #ifdef RT_OS_WINDOWS
|
---|
402 | RTPrintf(" -r | --register Installs the service.\n"
|
---|
403 | " -u | --unregister Uninstall service.\n");
|
---|
404 | #endif
|
---|
405 |
|
---|
406 | RTPrintf("\n"
|
---|
407 | "Service-specific options:\n");
|
---|
408 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
409 | {
|
---|
410 | RTPrintf(" --enable-%-14s Enables the %s service. (default)\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
411 | RTPrintf(" --disable-%-13s Disables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
412 | RTPrintf(" --only-%-16s Only enables the %s service.\n", g_aServices[j].pDesc->pszName, g_aServices[j].pDesc->pszName);
|
---|
413 | if (g_aServices[j].pDesc->pszOptions)
|
---|
414 | RTPrintf("%s", g_aServices[j].pDesc->pszOptions);
|
---|
415 | }
|
---|
416 | RTPrintf("\n"
|
---|
417 | " Copyright (C) 2009-" VBOX_C_YEAR " " VBOX_VENDOR "\n");
|
---|
418 |
|
---|
419 | return 1;
|
---|
420 | }
|
---|
421 |
|
---|
422 |
|
---|
423 | /**
|
---|
424 | * Displays an error message.
|
---|
425 | *
|
---|
426 | * @returns RTEXITCODE_FAILURE.
|
---|
427 | * @param pszFormat The message text.
|
---|
428 | * @param ... Format arguments.
|
---|
429 | */
|
---|
430 | RTEXITCODE VGSvcError(const char *pszFormat, ...)
|
---|
431 | {
|
---|
432 | va_list args;
|
---|
433 | va_start(args, pszFormat);
|
---|
434 | char *psz = NULL;
|
---|
435 | RTStrAPrintfV(&psz, pszFormat, args);
|
---|
436 | va_end(args);
|
---|
437 |
|
---|
438 | AssertPtr(psz);
|
---|
439 | LogRel(("Error: %s", psz));
|
---|
440 |
|
---|
441 | RTStrFree(psz);
|
---|
442 |
|
---|
443 | return RTEXITCODE_FAILURE;
|
---|
444 | }
|
---|
445 |
|
---|
446 |
|
---|
447 | /**
|
---|
448 | * Displays a verbose message based on the currently
|
---|
449 | * set global verbosity level.
|
---|
450 | *
|
---|
451 | * @param iLevel Minimum log level required to display this message.
|
---|
452 | * @param pszFormat The message text.
|
---|
453 | * @param ... Format arguments.
|
---|
454 | */
|
---|
455 | void VGSvcVerbose(unsigned iLevel, const char *pszFormat, ...)
|
---|
456 | {
|
---|
457 | if (iLevel <= g_cVerbosity)
|
---|
458 | {
|
---|
459 | va_list va;
|
---|
460 | va_start(va, pszFormat);
|
---|
461 | VGSvcLogV(pszFormat, va);
|
---|
462 | va_end(va);
|
---|
463 | }
|
---|
464 | }
|
---|
465 |
|
---|
466 |
|
---|
467 | /**
|
---|
468 | * Reports the current VBoxService status to the host.
|
---|
469 | *
|
---|
470 | * This makes sure that the Failed state is sticky.
|
---|
471 | *
|
---|
472 | * @return IPRT status code.
|
---|
473 | * @param enmStatus Status to report to the host.
|
---|
474 | */
|
---|
475 | int VGSvcReportStatus(VBoxGuestFacilityStatus enmStatus)
|
---|
476 | {
|
---|
477 | /*
|
---|
478 | * VBoxGuestFacilityStatus_Failed is sticky.
|
---|
479 | */
|
---|
480 | static VBoxGuestFacilityStatus s_enmLastStatus = VBoxGuestFacilityStatus_Inactive;
|
---|
481 | VGSvcVerbose(4, "Setting VBoxService status to %u\n", enmStatus);
|
---|
482 | if (s_enmLastStatus != VBoxGuestFacilityStatus_Failed)
|
---|
483 | {
|
---|
484 | int rc = VbglR3ReportAdditionsStatus(VBoxGuestFacilityType_VBoxService, enmStatus, 0 /* Flags */);
|
---|
485 | if (RT_FAILURE(rc))
|
---|
486 | {
|
---|
487 | VGSvcError("Could not report VBoxService status (%u), rc=%Rrc\n", enmStatus, rc);
|
---|
488 | return rc;
|
---|
489 | }
|
---|
490 | s_enmLastStatus = enmStatus;
|
---|
491 | }
|
---|
492 | return VINF_SUCCESS;
|
---|
493 | }
|
---|
494 |
|
---|
495 |
|
---|
496 | /**
|
---|
497 | * Gets a 32-bit value argument.
|
---|
498 | * @todo Get rid of this and VGSvcArgString() as soon as we have RTOpt handling.
|
---|
499 | *
|
---|
500 | * @returns 0 on success, non-zero exit code on error.
|
---|
501 | * @param argc The argument count.
|
---|
502 | * @param argv The argument vector
|
---|
503 | * @param psz Where in *pi to start looking for the value argument.
|
---|
504 | * @param pi Where to find and perhaps update the argument index.
|
---|
505 | * @param pu32 Where to store the 32-bit value.
|
---|
506 | * @param u32Min The minimum value.
|
---|
507 | * @param u32Max The maximum value.
|
---|
508 | */
|
---|
509 | int VGSvcArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max)
|
---|
510 | {
|
---|
511 | if (*psz == ':' || *psz == '=')
|
---|
512 | psz++;
|
---|
513 | if (!*psz)
|
---|
514 | {
|
---|
515 | if (*pi + 1 >= argc)
|
---|
516 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing value for the '%s' argument\n", argv[*pi]);
|
---|
517 | psz = argv[++*pi];
|
---|
518 | }
|
---|
519 |
|
---|
520 | char *pszNext;
|
---|
521 | int rc = RTStrToUInt32Ex(psz, &pszNext, 0, pu32);
|
---|
522 | if (RT_FAILURE(rc) || *pszNext)
|
---|
523 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Failed to convert interval '%s' to a number\n", psz);
|
---|
524 | if (*pu32 < u32Min || *pu32 > u32Max)
|
---|
525 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "The timesync interval of %RU32 seconds is out of range [%RU32..%RU32]\n",
|
---|
526 | *pu32, u32Min, u32Max);
|
---|
527 | return 0;
|
---|
528 | }
|
---|
529 |
|
---|
530 |
|
---|
531 | /** @todo Get rid of this and VGSvcArgUInt32() as soon as we have RTOpt handling. */
|
---|
532 | static int vgsvcArgString(int argc, char **argv, const char *psz, int *pi, char *pszBuf, size_t cbBuf)
|
---|
533 | {
|
---|
534 | AssertPtrReturn(pszBuf, VERR_INVALID_POINTER);
|
---|
535 | AssertReturn(cbBuf, VERR_INVALID_PARAMETER);
|
---|
536 |
|
---|
537 | if (*psz == ':' || *psz == '=')
|
---|
538 | psz++;
|
---|
539 | if (!*psz)
|
---|
540 | {
|
---|
541 | if (*pi + 1 >= argc)
|
---|
542 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Missing string for the '%s' argument\n", argv[*pi]);
|
---|
543 | psz = argv[++*pi];
|
---|
544 | }
|
---|
545 |
|
---|
546 | if (!RTStrPrintf(pszBuf, cbBuf, "%s", psz))
|
---|
547 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "String for '%s' argument too big\n", argv[*pi]);
|
---|
548 | return 0;
|
---|
549 | }
|
---|
550 |
|
---|
551 |
|
---|
552 | /**
|
---|
553 | * The service thread.
|
---|
554 | *
|
---|
555 | * @returns Whatever the worker function returns.
|
---|
556 | * @param ThreadSelf My thread handle.
|
---|
557 | * @param pvUser The service index.
|
---|
558 | */
|
---|
559 | static DECLCALLBACK(int) vgsvcThread(RTTHREAD ThreadSelf, void *pvUser)
|
---|
560 | {
|
---|
561 | const unsigned i = (uintptr_t)pvUser;
|
---|
562 |
|
---|
563 | #ifndef RT_OS_WINDOWS
|
---|
564 | /*
|
---|
565 | * Block all signals for this thread. Only the main thread will handle signals.
|
---|
566 | */
|
---|
567 | sigset_t signalMask;
|
---|
568 | sigfillset(&signalMask);
|
---|
569 | pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
|
---|
570 | #endif
|
---|
571 |
|
---|
572 | int rc = g_aServices[i].pDesc->pfnWorker(&g_aServices[i].fShutdown);
|
---|
573 | ASMAtomicXchgBool(&g_aServices[i].fShutdown, true);
|
---|
574 | RTThreadUserSignal(ThreadSelf);
|
---|
575 | return rc;
|
---|
576 | }
|
---|
577 |
|
---|
578 |
|
---|
579 | /**
|
---|
580 | * Lazily calls the pfnPreInit method on each service.
|
---|
581 | *
|
---|
582 | * @returns VBox status code, error message displayed.
|
---|
583 | */
|
---|
584 | static RTEXITCODE vgsvcLazyPreInit(void)
|
---|
585 | {
|
---|
586 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
587 | if (!g_aServices[j].fPreInited)
|
---|
588 | {
|
---|
589 | int rc = g_aServices[j].pDesc->pfnPreInit();
|
---|
590 | if (RT_FAILURE(rc))
|
---|
591 | return VGSvcError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
|
---|
592 | g_aServices[j].fPreInited = true;
|
---|
593 | }
|
---|
594 | return RTEXITCODE_SUCCESS;
|
---|
595 | }
|
---|
596 |
|
---|
597 |
|
---|
598 | /**
|
---|
599 | * Count the number of enabled services.
|
---|
600 | */
|
---|
601 | static unsigned vgsvcCountEnabledServices(void)
|
---|
602 | {
|
---|
603 | unsigned cEnabled = 0;
|
---|
604 | for (unsigned i = 0; i < RT_ELEMENTS(g_aServices); i++)
|
---|
605 | cEnabled += g_aServices[i].fEnabled;
|
---|
606 | return cEnabled;
|
---|
607 | }
|
---|
608 |
|
---|
609 |
|
---|
610 | #ifdef RT_OS_WINDOWS
|
---|
611 | /**
|
---|
612 | * Console control event callback.
|
---|
613 | *
|
---|
614 | * @returns TRUE if handled, FALSE if not.
|
---|
615 | * @param dwCtrlType The control event type.
|
---|
616 | *
|
---|
617 | * @remarks This is generally called on a new thread, so we're racing every
|
---|
618 | * other thread in the process.
|
---|
619 | */
|
---|
620 | static BOOL WINAPI vgsvcWinConsoleControlHandler(DWORD dwCtrlType)
|
---|
621 | {
|
---|
622 | int rc = VINF_SUCCESS;
|
---|
623 | bool fEventHandled = FALSE;
|
---|
624 | switch (dwCtrlType)
|
---|
625 | {
|
---|
626 | /* User pressed CTRL+C or CTRL+BREAK or an external event was sent
|
---|
627 | * via GenerateConsoleCtrlEvent(). */
|
---|
628 | case CTRL_BREAK_EVENT:
|
---|
629 | case CTRL_CLOSE_EVENT:
|
---|
630 | case CTRL_C_EVENT:
|
---|
631 | VGSvcVerbose(2, "ControlHandler: Received break/close event\n");
|
---|
632 | rc = VGSvcStopServices();
|
---|
633 | fEventHandled = TRUE;
|
---|
634 | break;
|
---|
635 | default:
|
---|
636 | break;
|
---|
637 | /** @todo Add other events here. */
|
---|
638 | }
|
---|
639 |
|
---|
640 | if (RT_FAILURE(rc))
|
---|
641 | VGSvcError("ControlHandler: Event %ld handled with error rc=%Rrc\n",
|
---|
642 | dwCtrlType, rc);
|
---|
643 | return fEventHandled;
|
---|
644 | }
|
---|
645 | #endif /* RT_OS_WINDOWS */
|
---|
646 |
|
---|
647 |
|
---|
648 | /**
|
---|
649 | * Starts the service.
|
---|
650 | *
|
---|
651 | * @returns VBox status code, errors are fully bitched.
|
---|
652 | *
|
---|
653 | * @remarks Also called from VBoxService-win.cpp, thus not static.
|
---|
654 | */
|
---|
655 | int VGSvcStartServices(void)
|
---|
656 | {
|
---|
657 | int rc;
|
---|
658 |
|
---|
659 | VGSvcReportStatus(VBoxGuestFacilityStatus_Init);
|
---|
660 |
|
---|
661 | /*
|
---|
662 | * Initialize the services.
|
---|
663 | */
|
---|
664 | VGSvcVerbose(2, "Initializing services ...\n");
|
---|
665 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
666 | if (g_aServices[j].fEnabled)
|
---|
667 | {
|
---|
668 | rc = g_aServices[j].pDesc->pfnInit();
|
---|
669 | if (RT_FAILURE(rc))
|
---|
670 | {
|
---|
671 | if (rc != VERR_SERVICE_DISABLED)
|
---|
672 | {
|
---|
673 | VGSvcError("Service '%s' failed to initialize: %Rrc\n", g_aServices[j].pDesc->pszName, rc);
|
---|
674 | VGSvcReportStatus(VBoxGuestFacilityStatus_Failed);
|
---|
675 | return rc;
|
---|
676 | }
|
---|
677 |
|
---|
678 | g_aServices[j].fEnabled = false;
|
---|
679 | VGSvcVerbose(0, "Service '%s' was disabled because of missing functionality\n", g_aServices[j].pDesc->pszName);
|
---|
680 | }
|
---|
681 | }
|
---|
682 |
|
---|
683 | /*
|
---|
684 | * Start the service(s).
|
---|
685 | */
|
---|
686 | VGSvcVerbose(2, "Starting services ...\n");
|
---|
687 | rc = VINF_SUCCESS;
|
---|
688 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
689 | {
|
---|
690 | if (!g_aServices[j].fEnabled)
|
---|
691 | continue;
|
---|
692 |
|
---|
693 | VGSvcVerbose(2, "Starting service '%s' ...\n", g_aServices[j].pDesc->pszName);
|
---|
694 | rc = RTThreadCreate(&g_aServices[j].Thread, vgsvcThread, (void *)(uintptr_t)j, 0,
|
---|
695 | RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
|
---|
696 | if (RT_FAILURE(rc))
|
---|
697 | {
|
---|
698 | VGSvcError("RTThreadCreate failed, rc=%Rrc\n", rc);
|
---|
699 | break;
|
---|
700 | }
|
---|
701 | g_aServices[j].fStarted = true;
|
---|
702 |
|
---|
703 | /* Wait for the thread to initialize. */
|
---|
704 | /** @todo There is a race between waiting and checking
|
---|
705 | * the fShutdown flag of a thread here and processing
|
---|
706 | * the thread's actual worker loop. If the thread decides
|
---|
707 | * to exit the loop before we skipped the fShutdown check
|
---|
708 | * below the service will fail to start! */
|
---|
709 | /** @todo This presumably means either a one-shot service or that
|
---|
710 | * something has gone wrong. In the second case treating it as failure
|
---|
711 | * to start is probably right, so we need a way to signal the first
|
---|
712 | * rather than leaving the idle thread hanging around. A flag in the
|
---|
713 | * service description? */
|
---|
714 | RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
|
---|
715 | if (g_aServices[j].fShutdown)
|
---|
716 | {
|
---|
717 | VGSvcError("Service '%s' failed to start!\n", g_aServices[j].pDesc->pszName);
|
---|
718 | rc = VERR_GENERAL_FAILURE;
|
---|
719 | }
|
---|
720 | }
|
---|
721 |
|
---|
722 | if (RT_SUCCESS(rc))
|
---|
723 | VGSvcVerbose(1, "All services started.\n");
|
---|
724 | else
|
---|
725 | {
|
---|
726 | VGSvcError("An error occcurred while the services!\n");
|
---|
727 | VGSvcReportStatus(VBoxGuestFacilityStatus_Failed);
|
---|
728 | }
|
---|
729 | return rc;
|
---|
730 | }
|
---|
731 |
|
---|
732 |
|
---|
733 | /**
|
---|
734 | * Stops and terminates the services.
|
---|
735 | *
|
---|
736 | * This should be called even when VBoxServiceStartServices fails so it can
|
---|
737 | * clean up anything that we succeeded in starting.
|
---|
738 | *
|
---|
739 | * @remarks Also called from VBoxService-win.cpp, thus not static.
|
---|
740 | */
|
---|
741 | int VGSvcStopServices(void)
|
---|
742 | {
|
---|
743 | VGSvcReportStatus(VBoxGuestFacilityStatus_Terminating);
|
---|
744 |
|
---|
745 | /*
|
---|
746 | * Signal all the services.
|
---|
747 | */
|
---|
748 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
749 | ASMAtomicWriteBool(&g_aServices[j].fShutdown, true);
|
---|
750 |
|
---|
751 | /*
|
---|
752 | * Do the pfnStop callback on all running services.
|
---|
753 | */
|
---|
754 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
755 | if (g_aServices[j].fStarted)
|
---|
756 | {
|
---|
757 | VGSvcVerbose(3, "Calling stop function for service '%s' ...\n", g_aServices[j].pDesc->pszName);
|
---|
758 | g_aServices[j].pDesc->pfnStop();
|
---|
759 | }
|
---|
760 |
|
---|
761 | VGSvcVerbose(3, "All stop functions for services called\n");
|
---|
762 |
|
---|
763 | /*
|
---|
764 | * Wait for all the service threads to complete.
|
---|
765 | */
|
---|
766 | int rc = VINF_SUCCESS;
|
---|
767 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
768 | {
|
---|
769 | if (!g_aServices[j].fEnabled) /* Only stop services which were started before. */
|
---|
770 | continue;
|
---|
771 | if (g_aServices[j].Thread != NIL_RTTHREAD)
|
---|
772 | {
|
---|
773 | VGSvcVerbose(2, "Waiting for service '%s' to stop ...\n", g_aServices[j].pDesc->pszName);
|
---|
774 | int rc2 = VINF_SUCCESS;
|
---|
775 | for (int i = 0; i < 30; i++) /* Wait 30 seconds in total */
|
---|
776 | {
|
---|
777 | rc2 = RTThreadWait(g_aServices[j].Thread, 1000 /* Wait 1 second */, NULL);
|
---|
778 | if (RT_SUCCESS(rc2))
|
---|
779 | break;
|
---|
780 | #ifdef RT_OS_WINDOWS
|
---|
781 | /* Notify SCM that it takes a bit longer ... */
|
---|
782 | VGSvcWinSetStopPendingStatus(i + j*32);
|
---|
783 | #endif
|
---|
784 | }
|
---|
785 | if (RT_FAILURE(rc2))
|
---|
786 | {
|
---|
787 | VGSvcError("Service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc2);
|
---|
788 | rc = rc2;
|
---|
789 | }
|
---|
790 | }
|
---|
791 | VGSvcVerbose(3, "Terminating service '%s' (%d) ...\n", g_aServices[j].pDesc->pszName, j);
|
---|
792 | g_aServices[j].pDesc->pfnTerm();
|
---|
793 | }
|
---|
794 |
|
---|
795 | #ifdef RT_OS_WINDOWS
|
---|
796 | /*
|
---|
797 | * Wake up and tell the main() thread that we're shutting down (it's
|
---|
798 | * sleeping in VBoxServiceMainWait).
|
---|
799 | */
|
---|
800 | ASMAtomicWriteBool(&g_fWindowsServiceShutdown, true);
|
---|
801 | if (g_hEvtWindowsService != NIL_RTSEMEVENT)
|
---|
802 | {
|
---|
803 | VGSvcVerbose(3, "Stopping the main thread...\n");
|
---|
804 | int rc2 = RTSemEventSignal(g_hEvtWindowsService);
|
---|
805 | AssertRC(rc2);
|
---|
806 | }
|
---|
807 | #endif
|
---|
808 |
|
---|
809 | VGSvcVerbose(2, "Stopping services returning: %Rrc\n", rc);
|
---|
810 | VGSvcReportStatus(RT_SUCCESS(rc) ? VBoxGuestFacilityStatus_Paused : VBoxGuestFacilityStatus_Failed);
|
---|
811 | return rc;
|
---|
812 | }
|
---|
813 |
|
---|
814 |
|
---|
815 | /**
|
---|
816 | * Block the main thread until the service shuts down.
|
---|
817 | *
|
---|
818 | * @remarks Also called from VBoxService-win.cpp, thus not static.
|
---|
819 | */
|
---|
820 | void VGSvcMainWait(void)
|
---|
821 | {
|
---|
822 | int rc;
|
---|
823 |
|
---|
824 | VGSvcReportStatus(VBoxGuestFacilityStatus_Active);
|
---|
825 |
|
---|
826 | #ifdef RT_OS_WINDOWS
|
---|
827 | /*
|
---|
828 | * Wait for the semaphore to be signalled.
|
---|
829 | */
|
---|
830 | VGSvcVerbose(1, "Waiting in main thread\n");
|
---|
831 | rc = RTSemEventCreate(&g_hEvtWindowsService);
|
---|
832 | AssertRC(rc);
|
---|
833 | while (!ASMAtomicReadBool(&g_fWindowsServiceShutdown))
|
---|
834 | {
|
---|
835 | rc = RTSemEventWait(g_hEvtWindowsService, RT_INDEFINITE_WAIT);
|
---|
836 | AssertRC(rc);
|
---|
837 | }
|
---|
838 | RTSemEventDestroy(g_hEvtWindowsService);
|
---|
839 | g_hEvtWindowsService = NIL_RTSEMEVENT;
|
---|
840 | #else
|
---|
841 | /*
|
---|
842 | * Wait explicitly for a HUP, INT, QUIT, ABRT or TERM signal, blocking
|
---|
843 | * all important signals.
|
---|
844 | *
|
---|
845 | * The annoying EINTR/ERESTART loop is for the benefit of Solaris where
|
---|
846 | * sigwait returns when we receive a SIGCHLD. Kind of makes sense since
|
---|
847 | * the signal has to be delivered... Anyway, darwin (10.9.5) has a much
|
---|
848 | * worse way of dealing with SIGCHLD, apparently it'll just return any
|
---|
849 | * of the signals we're waiting on when SIGCHLD becomes pending on this
|
---|
850 | * thread. So, we wait for SIGCHLD here and ignores it.
|
---|
851 | */
|
---|
852 | sigset_t signalMask;
|
---|
853 | sigemptyset(&signalMask);
|
---|
854 | sigaddset(&signalMask, SIGHUP);
|
---|
855 | sigaddset(&signalMask, SIGINT);
|
---|
856 | sigaddset(&signalMask, SIGQUIT);
|
---|
857 | sigaddset(&signalMask, SIGABRT);
|
---|
858 | sigaddset(&signalMask, SIGTERM);
|
---|
859 | sigaddset(&signalMask, SIGCHLD);
|
---|
860 | pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
|
---|
861 |
|
---|
862 | int iSignal;
|
---|
863 | do
|
---|
864 | {
|
---|
865 | iSignal = -1;
|
---|
866 | rc = sigwait(&signalMask, &iSignal);
|
---|
867 | }
|
---|
868 | while ( rc == EINTR
|
---|
869 | # ifdef ERESTART
|
---|
870 | || rc == ERESTART
|
---|
871 | # endif
|
---|
872 | || iSignal == SIGCHLD
|
---|
873 | );
|
---|
874 |
|
---|
875 | VGSvcVerbose(3, "VGSvcMainWait: Received signal %d (rc=%d)\n", iSignal, rc);
|
---|
876 | #endif /* !RT_OS_WINDOWS */
|
---|
877 | }
|
---|
878 |
|
---|
879 |
|
---|
880 | int main(int argc, char **argv)
|
---|
881 | {
|
---|
882 | RTEXITCODE rcExit;
|
---|
883 |
|
---|
884 | /*
|
---|
885 | * Init globals and such.
|
---|
886 | */
|
---|
887 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
888 | if (RT_FAILURE(rc))
|
---|
889 | return RTMsgInitFailure(rc);
|
---|
890 | g_pszProgName = RTPathFilename(argv[0]);
|
---|
891 | #ifdef RT_OS_WINDOWS
|
---|
892 | VGSvcWinResolveApis();
|
---|
893 | #endif
|
---|
894 | #ifdef DEBUG
|
---|
895 | rc = RTCritSectInit(&g_csLog);
|
---|
896 | AssertRC(rc);
|
---|
897 | #endif
|
---|
898 |
|
---|
899 | #ifdef VBOX_WITH_VBOXSERVICE_TOOLBOX
|
---|
900 | /*
|
---|
901 | * Run toolbox code before all other stuff since these things are simpler
|
---|
902 | * shell/file/text utility like programs that just happens to be inside
|
---|
903 | * VBoxService and shouldn't be subject to /dev/vboxguest, pid-files and
|
---|
904 | * global mutex restrictions.
|
---|
905 | */
|
---|
906 | if (VGSvcToolboxMain(argc, argv, &rcExit))
|
---|
907 | return rcExit;
|
---|
908 | #endif
|
---|
909 |
|
---|
910 | bool fUserSession = false;
|
---|
911 | #ifdef VBOX_WITH_VBOXSERVICE_CONTROL
|
---|
912 | /*
|
---|
913 | * Check if we're the specially spawned VBoxService.exe process that
|
---|
914 | * handles a guest control session.
|
---|
915 | */
|
---|
916 | if ( argc >= 2
|
---|
917 | && !RTStrICmp(argv[1], VBOXSERVICECTRLSESSION_GETOPT_PREFIX))
|
---|
918 | fUserSession = true;
|
---|
919 | #endif
|
---|
920 |
|
---|
921 | /*
|
---|
922 | * Connect to the kernel part before daemonizing so we can fail and
|
---|
923 | * complain if there is some kind of problem. We need to initialize the
|
---|
924 | * guest lib *before* we do the pre-init just in case one of services needs
|
---|
925 | * do to some initial stuff with it.
|
---|
926 | */
|
---|
927 | if (fUserSession)
|
---|
928 | rc = VbglR3InitUser();
|
---|
929 | else
|
---|
930 | rc = VbglR3Init();
|
---|
931 | if (RT_FAILURE(rc))
|
---|
932 | {
|
---|
933 | if (rc == VERR_ACCESS_DENIED)
|
---|
934 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Insufficient privileges to start %s! Please start with Administrator/root privileges!\n",
|
---|
935 | g_pszProgName);
|
---|
936 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "VbglR3Init failed with rc=%Rrc\n", rc);
|
---|
937 | }
|
---|
938 |
|
---|
939 | #ifdef RT_OS_WINDOWS
|
---|
940 | /*
|
---|
941 | * Check if we're the specially spawned VBoxService.exe process that
|
---|
942 | * handles page fusion. This saves an extra statically linked executable.
|
---|
943 | */
|
---|
944 | if ( argc == 2
|
---|
945 | && !RTStrICmp(argv[1], "pagefusion"))
|
---|
946 | return VGSvcPageSharingWorkerChild();
|
---|
947 | #endif
|
---|
948 |
|
---|
949 | #ifdef VBOX_WITH_VBOXSERVICE_CONTROL
|
---|
950 | /*
|
---|
951 | * Check if we're the specially spawned VBoxService.exe process that
|
---|
952 | * handles a guest control session.
|
---|
953 | */
|
---|
954 | if (fUserSession)
|
---|
955 | return VGSvcGstCtrlSessionSpawnInit(argc, argv);
|
---|
956 | #endif
|
---|
957 |
|
---|
958 | /*
|
---|
959 | * Parse the arguments.
|
---|
960 | *
|
---|
961 | * Note! This code predates RTGetOpt, thus the manual parsing.
|
---|
962 | */
|
---|
963 | bool fDaemonize = true;
|
---|
964 | bool fDaemonized = false;
|
---|
965 | for (int i = 1; i < argc; i++)
|
---|
966 | {
|
---|
967 | const char *psz = argv[i];
|
---|
968 | if (*psz != '-')
|
---|
969 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown argument '%s'\n", psz);
|
---|
970 | psz++;
|
---|
971 |
|
---|
972 | /* translate long argument to short */
|
---|
973 | if (*psz == '-')
|
---|
974 | {
|
---|
975 | psz++;
|
---|
976 | size_t cch = strlen(psz);
|
---|
977 | #define MATCHES(strconst) ( cch == sizeof(strconst) - 1 \
|
---|
978 | && !memcmp(psz, strconst, sizeof(strconst) - 1) )
|
---|
979 | if (MATCHES("foreground"))
|
---|
980 | psz = "f";
|
---|
981 | else if (MATCHES("verbose"))
|
---|
982 | psz = "v";
|
---|
983 | else if (MATCHES("version"))
|
---|
984 | psz = "V";
|
---|
985 | else if (MATCHES("help"))
|
---|
986 | psz = "h";
|
---|
987 | else if (MATCHES("interval"))
|
---|
988 | psz = "i";
|
---|
989 | #ifdef RT_OS_WINDOWS
|
---|
990 | else if (MATCHES("register"))
|
---|
991 | psz = "r";
|
---|
992 | else if (MATCHES("unregister"))
|
---|
993 | psz = "u";
|
---|
994 | #endif
|
---|
995 | else if (MATCHES("logfile"))
|
---|
996 | psz = "l";
|
---|
997 | else if (MATCHES("pidfile"))
|
---|
998 | psz = "p";
|
---|
999 | else if (MATCHES("daemonized"))
|
---|
1000 | {
|
---|
1001 | fDaemonized = true;
|
---|
1002 | continue;
|
---|
1003 | }
|
---|
1004 | else
|
---|
1005 | {
|
---|
1006 | bool fFound = false;
|
---|
1007 |
|
---|
1008 | if (cch > sizeof("enable-") && !memcmp(psz, RT_STR_TUPLE("enable-")))
|
---|
1009 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
1010 | if ((fFound = !RTStrICmp(psz + sizeof("enable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
1011 | g_aServices[j].fEnabled = true;
|
---|
1012 |
|
---|
1013 | if (cch > sizeof("disable-") && !memcmp(psz, RT_STR_TUPLE("disable-")))
|
---|
1014 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
1015 | if ((fFound = !RTStrICmp(psz + sizeof("disable-") - 1, g_aServices[j].pDesc->pszName)))
|
---|
1016 | g_aServices[j].fEnabled = false;
|
---|
1017 |
|
---|
1018 | if (cch > sizeof("only-") && !memcmp(psz, RT_STR_TUPLE("only-")))
|
---|
1019 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
1020 | {
|
---|
1021 | g_aServices[j].fEnabled = !RTStrICmp(psz + sizeof("only-") - 1, g_aServices[j].pDesc->pszName);
|
---|
1022 | if (g_aServices[j].fEnabled)
|
---|
1023 | fFound = true;
|
---|
1024 | }
|
---|
1025 |
|
---|
1026 | if (!fFound)
|
---|
1027 | {
|
---|
1028 | rcExit = vgsvcLazyPreInit();
|
---|
1029 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
1030 | return rcExit;
|
---|
1031 | for (unsigned j = 0; !fFound && j < RT_ELEMENTS(g_aServices); j++)
|
---|
1032 | {
|
---|
1033 | rc = g_aServices[j].pDesc->pfnOption(NULL, argc, argv, &i);
|
---|
1034 | fFound = rc == VINF_SUCCESS;
|
---|
1035 | if (fFound)
|
---|
1036 | break;
|
---|
1037 | if (rc != -1)
|
---|
1038 | return rc;
|
---|
1039 | }
|
---|
1040 | }
|
---|
1041 | if (!fFound)
|
---|
1042 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown option '%s'\n", argv[i]);
|
---|
1043 | continue;
|
---|
1044 | }
|
---|
1045 | #undef MATCHES
|
---|
1046 | }
|
---|
1047 |
|
---|
1048 | /* handle the string of short options. */
|
---|
1049 | do
|
---|
1050 | {
|
---|
1051 | switch (*psz)
|
---|
1052 | {
|
---|
1053 | case 'i':
|
---|
1054 | rc = VGSvcArgUInt32(argc, argv, psz + 1, &i,
|
---|
1055 | &g_DefaultInterval, 1, (UINT32_MAX / 1000) - 1);
|
---|
1056 | if (rc)
|
---|
1057 | return rc;
|
---|
1058 | psz = NULL;
|
---|
1059 | break;
|
---|
1060 |
|
---|
1061 | case 'f':
|
---|
1062 | fDaemonize = false;
|
---|
1063 | break;
|
---|
1064 |
|
---|
1065 | case 'v':
|
---|
1066 | g_cVerbosity++;
|
---|
1067 | break;
|
---|
1068 |
|
---|
1069 | case 'V':
|
---|
1070 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
1071 | return RTEXITCODE_SUCCESS;
|
---|
1072 |
|
---|
1073 | case 'h':
|
---|
1074 | case '?':
|
---|
1075 | return vgsvcUsage();
|
---|
1076 |
|
---|
1077 | #ifdef RT_OS_WINDOWS
|
---|
1078 | case 'r':
|
---|
1079 | return VGSvcWinInstall();
|
---|
1080 |
|
---|
1081 | case 'u':
|
---|
1082 | return VGSvcWinUninstall();
|
---|
1083 | #endif
|
---|
1084 |
|
---|
1085 | case 'l':
|
---|
1086 | {
|
---|
1087 | rc = vgsvcArgString(argc, argv, psz + 1, &i,
|
---|
1088 | g_szLogFile, sizeof(g_szLogFile));
|
---|
1089 | if (rc)
|
---|
1090 | return rc;
|
---|
1091 | psz = NULL;
|
---|
1092 | break;
|
---|
1093 | }
|
---|
1094 |
|
---|
1095 | case 'p':
|
---|
1096 | {
|
---|
1097 | rc = vgsvcArgString(argc, argv, psz + 1, &i,
|
---|
1098 | g_szPidFile, sizeof(g_szPidFile));
|
---|
1099 | if (rc)
|
---|
1100 | return rc;
|
---|
1101 | psz = NULL;
|
---|
1102 | break;
|
---|
1103 | }
|
---|
1104 |
|
---|
1105 | default:
|
---|
1106 | {
|
---|
1107 | rcExit = vgsvcLazyPreInit();
|
---|
1108 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
1109 | return rcExit;
|
---|
1110 |
|
---|
1111 | bool fFound = false;
|
---|
1112 | for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
|
---|
1113 | {
|
---|
1114 | rc = g_aServices[j].pDesc->pfnOption(&psz, argc, argv, &i);
|
---|
1115 | fFound = rc == VINF_SUCCESS;
|
---|
1116 | if (fFound)
|
---|
1117 | break;
|
---|
1118 | if (rc != -1)
|
---|
1119 | return rc;
|
---|
1120 | }
|
---|
1121 | if (!fFound)
|
---|
1122 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "Unknown option '%c' (%s)\n", *psz, argv[i]);
|
---|
1123 | break;
|
---|
1124 | }
|
---|
1125 | }
|
---|
1126 | } while (psz && *++psz);
|
---|
1127 | }
|
---|
1128 |
|
---|
1129 | /* Check that at least one service is enabled. */
|
---|
1130 | if (vgsvcCountEnabledServices() == 0)
|
---|
1131 | return RTMsgErrorExit(RTEXITCODE_SYNTAX, "At least one service must be enabled\n");
|
---|
1132 |
|
---|
1133 | rc = VGSvcLogCreate(g_szLogFile[0] ? g_szLogFile : NULL);
|
---|
1134 | if (RT_FAILURE(rc))
|
---|
1135 | return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to create release log '%s', rc=%Rrc\n",
|
---|
1136 | g_szLogFile[0] ? g_szLogFile : "<None>", rc);
|
---|
1137 |
|
---|
1138 | /* Call pre-init if we didn't do it already. */
|
---|
1139 | rcExit = vgsvcLazyPreInit();
|
---|
1140 | if (rcExit != RTEXITCODE_SUCCESS)
|
---|
1141 | return rcExit;
|
---|
1142 |
|
---|
1143 | #ifdef RT_OS_WINDOWS
|
---|
1144 | /*
|
---|
1145 | * Make sure only one instance of VBoxService runs at a time. Create a
|
---|
1146 | * global mutex for that.
|
---|
1147 | *
|
---|
1148 | * Note! The \\Global\ namespace was introduced with Win2K, thus the
|
---|
1149 | * version check.
|
---|
1150 | * Note! If the mutex exists CreateMutex will open it and set last error to
|
---|
1151 | * ERROR_ALREADY_EXISTS.
|
---|
1152 | */
|
---|
1153 | OSVERSIONINFOEX OSInfoEx;
|
---|
1154 | RT_ZERO(OSInfoEx);
|
---|
1155 | OSInfoEx.dwOSVersionInfoSize = sizeof(OSVERSIONINFOEX);
|
---|
1156 |
|
---|
1157 | SetLastError(NO_ERROR);
|
---|
1158 | HANDLE hMutexAppRunning;
|
---|
1159 | if ( GetVersionEx((LPOSVERSIONINFO)&OSInfoEx)
|
---|
1160 | && OSInfoEx.dwPlatformId == VER_PLATFORM_WIN32_NT
|
---|
1161 | && OSInfoEx.dwMajorVersion >= 5 /* NT 5.0 a.k.a W2K */)
|
---|
1162 | hMutexAppRunning = CreateMutex(NULL, FALSE, "Global\\" VBOXSERVICE_NAME);
|
---|
1163 | else
|
---|
1164 | hMutexAppRunning = CreateMutex(NULL, FALSE, VBOXSERVICE_NAME);
|
---|
1165 | if (hMutexAppRunning == NULL)
|
---|
1166 | {
|
---|
1167 | DWORD dwErr = GetLastError();
|
---|
1168 | if ( dwErr == ERROR_ALREADY_EXISTS
|
---|
1169 | || dwErr == ERROR_ACCESS_DENIED)
|
---|
1170 | {
|
---|
1171 | VGSvcError("%s is already running! Terminating.\n", g_pszProgName);
|
---|
1172 | return RTEXITCODE_FAILURE;
|
---|
1173 | }
|
---|
1174 |
|
---|
1175 | VGSvcError("CreateMutex failed with last error %u! Terminating.\n", GetLastError());
|
---|
1176 | return RTEXITCODE_FAILURE;
|
---|
1177 | }
|
---|
1178 |
|
---|
1179 | #else /* !RT_OS_WINDOWS */
|
---|
1180 | /** @todo Add PID file creation here? */
|
---|
1181 | #endif /* !RT_OS_WINDOWS */
|
---|
1182 |
|
---|
1183 | VGSvcVerbose(0, "%s r%s started. Verbose level = %d\n", RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
|
---|
1184 |
|
---|
1185 | /*
|
---|
1186 | * Daemonize if requested.
|
---|
1187 | */
|
---|
1188 | if (fDaemonize && !fDaemonized)
|
---|
1189 | {
|
---|
1190 | #ifdef RT_OS_WINDOWS
|
---|
1191 | VGSvcVerbose(2, "Starting service dispatcher ...\n");
|
---|
1192 | rcExit = VGSvcWinEnterCtrlDispatcher();
|
---|
1193 | #else
|
---|
1194 | VGSvcVerbose(1, "Daemonizing...\n");
|
---|
1195 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */,
|
---|
1196 | false /* fRespawn */, NULL /* pcRespawn */);
|
---|
1197 | if (RT_FAILURE(rc))
|
---|
1198 | return VGSvcError("Daemon failed: %Rrc\n", rc);
|
---|
1199 | /* in-child */
|
---|
1200 | #endif
|
---|
1201 | }
|
---|
1202 | #ifdef RT_OS_WINDOWS
|
---|
1203 | else
|
---|
1204 | #endif
|
---|
1205 | {
|
---|
1206 | /*
|
---|
1207 | * Windows: We're running the service as a console application now. Start the
|
---|
1208 | * services, enter the main thread's run loop and stop them again
|
---|
1209 | * when it returns.
|
---|
1210 | *
|
---|
1211 | * POSIX: This is used for both daemons and console runs. Start all services
|
---|
1212 | * and return immediately.
|
---|
1213 | */
|
---|
1214 | #ifdef RT_OS_WINDOWS
|
---|
1215 | # ifndef RT_OS_NT4 /** @todo r=bird: What's RT_OS_NT4??? */
|
---|
1216 | /* Install console control handler. */
|
---|
1217 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)vgsvcWinConsoleControlHandler, TRUE /* Add handler */))
|
---|
1218 | {
|
---|
1219 | VGSvcError("Unable to add console control handler, error=%ld\n", GetLastError());
|
---|
1220 | /* Just skip this error, not critical. */
|
---|
1221 | }
|
---|
1222 | # endif /* !RT_OS_NT4 */
|
---|
1223 | #endif /* RT_OS_WINDOWS */
|
---|
1224 | rc = VGSvcStartServices();
|
---|
1225 | RTFILE hPidFile = NIL_RTFILE;
|
---|
1226 | if (RT_SUCCESS(rc))
|
---|
1227 | if (g_szPidFile[0])
|
---|
1228 | rc = VbglR3PidFile(g_szPidFile, &hPidFile);
|
---|
1229 | rcExit = RT_SUCCESS(rc) ? RTEXITCODE_SUCCESS : RTEXITCODE_FAILURE;
|
---|
1230 | if (RT_SUCCESS(rc))
|
---|
1231 | VGSvcMainWait();
|
---|
1232 | if (g_szPidFile[0] && hPidFile != NIL_RTFILE)
|
---|
1233 | VbglR3ClosePidFile(g_szPidFile, hPidFile);
|
---|
1234 | #ifdef RT_OS_WINDOWS
|
---|
1235 | # ifndef RT_OS_NT4
|
---|
1236 | /* Uninstall console control handler. */
|
---|
1237 | if (!SetConsoleCtrlHandler((PHANDLER_ROUTINE)NULL, FALSE /* Remove handler */))
|
---|
1238 | {
|
---|
1239 | VGSvcError("Unable to remove console control handler, error=%ld\n", GetLastError());
|
---|
1240 | /* Just skip this error, not critical. */
|
---|
1241 | }
|
---|
1242 | # endif /* !RT_OS_NT4 */
|
---|
1243 | #else /* !RT_OS_WINDOWS */
|
---|
1244 | /* On Windows - since we're running as a console application - we already stopped all services
|
---|
1245 | * through the console control handler. So only do the stopping of services here on other platforms
|
---|
1246 | * where the break/shutdown/whatever signal was just received. */
|
---|
1247 | VGSvcStopServices();
|
---|
1248 | #endif /* RT_OS_WINDOWS */
|
---|
1249 | }
|
---|
1250 | VGSvcReportStatus(VBoxGuestFacilityStatus_Terminated);
|
---|
1251 |
|
---|
1252 | #ifdef RT_OS_WINDOWS
|
---|
1253 | /*
|
---|
1254 | * Cleanup mutex.
|
---|
1255 | */
|
---|
1256 | CloseHandle(hMutexAppRunning);
|
---|
1257 | #endif
|
---|
1258 |
|
---|
1259 | VGSvcVerbose(0, "Ended.\n");
|
---|
1260 |
|
---|
1261 | #ifdef DEBUG
|
---|
1262 | RTCritSectDelete(&g_csLog);
|
---|
1263 | //RTMemTrackerDumpAllToStdOut();
|
---|
1264 | #endif
|
---|
1265 |
|
---|
1266 | VGSvcLogDestroy();
|
---|
1267 |
|
---|
1268 | return rcExit;
|
---|
1269 | }
|
---|
1270 |
|
---|