VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/main.cpp@ 93371

Last change on this file since 93371 was 93371, checked in by vboxsync, 3 years ago

Additions: Linux: introduce VBoxClient --vmsvga-session service and connect it to VBoxDRMClient over IPC, bugref:10134.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id Revision
File size: 23.9 KB
Line 
1/* $Id: main.cpp 93371 2022-01-20 17:48:35Z vboxsync $ */
2/** @file
3 * VirtualBox Guest Additions - X11 Client.
4 */
5
6/*
7 * Copyright (C) 2006-2022 Oracle Corporation
8 *
9 * This file is part of VirtualBox Open Source Edition (OSE), as
10 * available from http://www.215389.xyz. 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* Header Files *
21*********************************************************************************************************************************/
22#include <sys/wait.h>
23#include <stdlib.h> /* For exit */
24#include <signal.h>
25#include <X11/Xlib.h>
26#include "product-generated.h"
27#include <iprt/asm.h>
28#include <iprt/buildconfig.h>
29#include <iprt/critsect.h>
30#include <iprt/errno.h>
31#include <iprt/getopt.h>
32#include <iprt/initterm.h>
33#include <iprt/message.h>
34#include <iprt/path.h>
35#include <iprt/stream.h>
36#include <VBox/VBoxGuestLib.h>
37#include <VBox/err.h>
38#include <VBox/version.h>
39#include "VBoxClient.h"
40
41
42/*********************************************************************************************************************************
43* Defines *
44*********************************************************************************************************************************/
45#define VBOXCLIENT_OPT_SERVICES 980
46#define VBOXCLIENT_OPT_CHECKHOSTVERSION VBOXCLIENT_OPT_SERVICES
47#define VBOXCLIENT_OPT_CLIPBOARD VBOXCLIENT_OPT_SERVICES + 1
48#define VBOXCLIENT_OPT_DRAGANDDROP VBOXCLIENT_OPT_SERVICES + 2
49#define VBOXCLIENT_OPT_SEAMLESS VBOXCLIENT_OPT_SERVICES + 3
50#define VBOXCLIENT_OPT_VMSVGA VBOXCLIENT_OPT_SERVICES + 4
51#define VBOXCLIENT_OPT_VMSVGA_SESSION VBOXCLIENT_OPT_SERVICES + 5
52
53
54/*********************************************************************************************************************************
55* Local structures *
56*********************************************************************************************************************************/
57/**
58 * The global service state.
59 */
60typedef struct VBCLSERVICESTATE
61{
62 /** Pointer to the service descriptor. */
63 PVBCLSERVICE pDesc;
64 /** The worker thread. NIL_RTTHREAD if it's the main thread. */
65 RTTHREAD Thread;
66 /** Whether Pre-init was called. */
67 bool fPreInited;
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} VBCLSERVICESTATE;
75/** Pointer to a service state. */
76typedef VBCLSERVICESTATE *PVBCLSERVICESTATE;
77
78
79/*********************************************************************************************************************************
80* Global Variables *
81*********************************************************************************************************************************/
82/** The global service state. */
83VBCLSERVICESTATE g_Service = { 0 };
84
85/** Set by the signal handler when being called. */
86static volatile bool g_fSignalHandlerCalled = false;
87/** Critical section for the signal handler. */
88static RTCRITSECT g_csSignalHandler;
89/** Flag indicating Whether the service starts in daemonized mode or not. */
90bool g_fDaemonized = false;
91/** The name of our pidfile. It is global for the benefit of the cleanup
92 * routine. */
93static char g_szPidFile[RTPATH_MAX] = "";
94/** The file handle of our pidfile. It is global for the benefit of the
95 * cleanup routine. */
96static RTFILE g_hPidFile;
97/** Global critical section held during the clean-up routine (to prevent it
98 * being called on multiple threads at once) or things which may not happen
99 * during clean-up (e.g. pausing and resuming the service).
100 */
101static RTCRITSECT g_critSect;
102/** Counter of how often our daemon has been respawned. */
103unsigned g_cRespawn = 0;
104/** Logging verbosity level. */
105unsigned g_cVerbosity = 0;
106/** Absolute path to log file, if any. */
107static char g_szLogFile[RTPATH_MAX + 128] = "";
108
109/**
110 * Shut down if we get a signal or something.
111 *
112 * This is extern so that we can call it from other compilation units.
113 */
114void VBClShutdown(bool fExit /*=true*/)
115{
116 /* We never release this, as we end up with a call to exit(3) which is not
117 * async-safe. Unless we fix this application properly, we should be sure
118 * never to exit from anywhere except from this method. */
119 int rc = RTCritSectEnter(&g_critSect);
120 if (RT_FAILURE(rc))
121 VBClLogFatalError("Failure while acquiring the global critical section, rc=%Rrc\n", rc);
122
123 /* Ask service to stop. */
124 if (g_Service.pDesc &&
125 g_Service.pDesc->pfnStop)
126 {
127 ASMAtomicWriteBool(&g_Service.fShutdown, true);
128 g_Service.pDesc->pfnStop();
129
130 }
131
132 if (g_szPidFile[0] && g_hPidFile)
133 VbglR3ClosePidFile(g_szPidFile, g_hPidFile);
134
135 VBClLogDestroy();
136
137 if (fExit)
138 exit(RTEXITCODE_SUCCESS);
139}
140
141/**
142 * Xlib error handler for certain errors that we can't avoid.
143 */
144static int vboxClientXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
145{
146 char errorText[1024];
147
148 XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
149 VBClLogError("An X Window protocol error occurred: %s (error code %d). Request code: %d, minor code: %d, serial number: %d\n", errorText, pError->error_code, pError->request_code, pError->minor_code, pError->serial);
150 return 0;
151}
152
153/**
154 * Xlib error handler for fatal errors. This often means that the programme is still running
155 * when X exits.
156 */
157static int vboxClientXLibIOErrorHandler(Display *pDisplay)
158{
159 RT_NOREF1(pDisplay);
160 VBClLogError("A fatal guest X Window error occurred. This may just mean that the Window system was shut down while the client was still running\n");
161 VBClShutdown();
162 return 0; /* We should never reach this. */
163}
164
165/**
166 * A standard signal handler which cleans up and exits.
167 */
168static void vboxClientSignalHandler(int iSignal)
169{
170 int rc = RTCritSectEnter(&g_csSignalHandler);
171 if (RT_SUCCESS(rc))
172 {
173 if (g_fSignalHandlerCalled)
174 {
175 RTCritSectLeave(&g_csSignalHandler);
176 return;
177 }
178
179 VBClLogVerbose(2, "Received signal %d\n", iSignal);
180 g_fSignalHandlerCalled = true;
181
182 /* Leave critical section before stopping the service. */
183 RTCritSectLeave(&g_csSignalHandler);
184
185 if ( g_Service.pDesc
186 && g_Service.pDesc->pfnStop)
187 {
188 VBClLogVerbose(2, "Notifying service to stop ...\n");
189
190 /* Signal the service to stop. */
191 ASMAtomicWriteBool(&g_Service.fShutdown, true);
192
193 g_Service.pDesc->pfnStop();
194
195 VBClLogVerbose(2, "Service notified to stop, waiting on worker thread to stop ...\n");
196 }
197 }
198}
199
200/**
201 * Reset all standard termination signals to call our signal handler.
202 */
203static int vboxClientSignalHandlerInstall(void)
204{
205 struct sigaction sigAction;
206 sigAction.sa_handler = vboxClientSignalHandler;
207 sigemptyset(&sigAction.sa_mask);
208 sigAction.sa_flags = 0;
209 sigaction(SIGHUP, &sigAction, NULL);
210 sigaction(SIGINT, &sigAction, NULL);
211 sigaction(SIGQUIT, &sigAction, NULL);
212 sigaction(SIGPIPE, &sigAction, NULL);
213 sigaction(SIGALRM, &sigAction, NULL);
214 sigaction(SIGTERM, &sigAction, NULL);
215 sigaction(SIGUSR1, &sigAction, NULL);
216 sigaction(SIGUSR2, &sigAction, NULL);
217
218 return RTCritSectInit(&g_csSignalHandler);
219}
220
221/**
222 * Uninstalls a previously installed signal handler.
223 */
224static int vboxClientSignalHandlerUninstall(void)
225{
226 signal(SIGTERM, SIG_DFL);
227#ifdef SIGBREAK
228 signal(SIGBREAK, SIG_DFL);
229#endif
230
231 return RTCritSectDelete(&g_csSignalHandler);
232}
233
234/**
235 * Print out a usage message and exit with success.
236 */
237static void vboxClientUsage(const char *pcszFileName)
238{
239 RTPrintf(VBOX_PRODUCT " VBoxClient "
240 VBOX_VERSION_STRING "\n"
241 "(C) 2005-" VBOX_C_YEAR " " VBOX_VENDOR "\n"
242 "All rights reserved.\n"
243 "\n");
244
245 RTPrintf("Usage: %s "
246#ifdef VBOX_WITH_SHARED_CLIPBOARD
247 "--clipboard|"
248#endif
249#ifdef VBOX_WITH_DRAG_AND_DROP
250 "--draganddrop|"
251#endif
252#ifdef VBOX_WITH_GUEST_PROPS
253 "--checkhostversion|"
254#endif
255#ifdef VBOX_WITH_SEAMLESS
256 "--seamless|"
257#endif
258#ifdef VBOX_WITH_VMSVGA
259 "--vmsvga|"
260 "--vmsvga-session"
261#endif
262 "\n[-d|--nodaemon]\n", pcszFileName);
263 RTPrintf("\n");
264 RTPrintf("Options:\n");
265#ifdef VBOX_WITH_SHARED_CLIPBOARD
266 RTPrintf(" --clipboard starts the shared clipboard service\n");
267#endif
268#ifdef VBOX_WITH_DRAG_AND_DROP
269 RTPrintf(" --draganddrop starts the drag and drop service\n");
270#endif
271#ifdef VBOX_WITH_GUEST_PROPS
272 RTPrintf(" --checkhostversion starts the host version notifier service\n");
273#endif
274#ifdef VBOX_WITH_SEAMLESS
275 RTPrintf(" --seamless starts the seamless windows service\n");
276#endif
277#ifdef VBOX_WITH_VMSVGA
278 RTPrintf(" --vmsvga starts VMSVGA dynamic resizing for X11/Wayland guests\n");
279 RTPrintf(" --vmsvga-session starts Desktop Environment specific screen assistant for X11/Wayland guests\n"
280 " (VMSVGA graphics adapter only)\n");
281#endif
282 RTPrintf(" -f, --foreground run in the foreground (no daemonizing)\n");
283 RTPrintf(" -d, --nodaemon continues running as a system service\n");
284 RTPrintf(" -h, --help shows this help text\n");
285 RTPrintf(" -l, --logfile <path> enables logging to a file\n");
286 RTPrintf(" -v, --verbose increases logging verbosity level\n");
287 RTPrintf(" -V, --version shows version information\n");
288 RTPrintf("\n");
289}
290
291/**
292 * Complains about seeing more than one service specification.
293 *
294 * @returns RTEXITCODE_SYNTAX.
295 */
296static int vbclSyntaxOnlyOneService(void)
297{
298 RTMsgError("More than one service specified! Only one, please.");
299 return RTEXITCODE_SYNTAX;
300}
301
302/**
303 * The service thread.
304 *
305 * @returns Whatever the worker function returns.
306 * @param ThreadSelf My thread handle.
307 * @param pvUser The service index.
308 */
309static DECLCALLBACK(int) vbclThread(RTTHREAD ThreadSelf, void *pvUser)
310{
311 PVBCLSERVICESTATE pState = (PVBCLSERVICESTATE)pvUser;
312 AssertPtrReturn(pState, VERR_INVALID_POINTER);
313
314#ifndef RT_OS_WINDOWS
315 /*
316 * Block all signals for this thread. Only the main thread will handle signals.
317 */
318 sigset_t signalMask;
319 sigfillset(&signalMask);
320 pthread_sigmask(SIG_BLOCK, &signalMask, NULL);
321#endif
322
323 AssertPtrReturn(pState->pDesc->pfnWorker, VERR_INVALID_POINTER);
324 int rc = pState->pDesc->pfnWorker(&pState->fShutdown);
325
326 VBClLogVerbose(2, "Worker loop ended with %Rrc\n", rc);
327
328 ASMAtomicXchgBool(&pState->fShutdown, true);
329 RTThreadUserSignal(ThreadSelf);
330 return rc;
331}
332
333/**
334 * The main loop for the VBoxClient daemon.
335 */
336int main(int argc, char *argv[])
337{
338 /* Note: No VBClLogXXX calls before actually creating the log. */
339
340 /* Initialize our runtime before all else. */
341 int rc = RTR3InitExe(argc, &argv, 0);
342 if (RT_FAILURE(rc))
343 return RTMsgInitFailure(rc);
344
345 /* This should never be called twice in one process - in fact one Display
346 * object should probably never be used from multiple threads anyway. */
347 if (!XInitThreads())
348 return RTMsgErrorExitFailure("Failed to initialize X11 threads\n");
349
350 /* Get our file name for usage info and hints. */
351 const char *pcszFileName = RTPathFilename(argv[0]);
352 if (!pcszFileName)
353 pcszFileName = "VBoxClient";
354
355 /* Parse our option(s). */
356 static const RTGETOPTDEF s_aOptions[] =
357 {
358 { "--nodaemon", 'd', RTGETOPT_REQ_NOTHING },
359 { "--foreground", 'f', RTGETOPT_REQ_NOTHING },
360 { "--help", 'h', RTGETOPT_REQ_NOTHING },
361 { "--logfile", 'l', RTGETOPT_REQ_STRING },
362 { "--version", 'V', RTGETOPT_REQ_NOTHING },
363 { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
364
365 /* Services */
366#ifdef VBOX_WITH_GUEST_PROPS
367 { "--checkhostversion", VBOXCLIENT_OPT_CHECKHOSTVERSION, RTGETOPT_REQ_NOTHING },
368#endif
369#ifdef VBOX_WITH_SHARED_CLIPBOARD
370 { "--clipboard", VBOXCLIENT_OPT_CLIPBOARD, RTGETOPT_REQ_NOTHING },
371#endif
372#ifdef VBOX_WITH_DRAG_AND_DROP
373 { "--draganddrop", VBOXCLIENT_OPT_DRAGANDDROP, RTGETOPT_REQ_NOTHING },
374#endif
375#ifdef VBOX_WITH_SEAMLESS
376 { "--seamless", VBOXCLIENT_OPT_SEAMLESS, RTGETOPT_REQ_NOTHING },
377#endif
378#ifdef VBOX_WITH_VMSVGA
379 { "--vmsvga", VBOXCLIENT_OPT_VMSVGA, RTGETOPT_REQ_NOTHING },
380 { "--vmsvga-session", VBOXCLIENT_OPT_VMSVGA_SESSION, RTGETOPT_REQ_NOTHING },
381#endif
382 };
383
384 int ch;
385 RTGETOPTUNION ValueUnion;
386 RTGETOPTSTATE GetState;
387 rc = RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, 0 /* fFlags */);
388 if (RT_FAILURE(rc))
389 return RTMsgErrorExitFailure("Failed to parse command line options, rc=%Rrc\n", rc);
390
391 AssertRC(rc);
392
393 bool fDaemonise = true;
394 bool fRespawn = true;
395
396 while ((ch = RTGetOpt(&GetState, &ValueUnion)) != 0)
397 {
398 /* For options that require an argument, ValueUnion has received the value. */
399 switch (ch)
400 {
401 case 'd':
402 {
403 fDaemonise = false;
404 break;
405 }
406
407 case 'h':
408 {
409 vboxClientUsage(pcszFileName);
410 return RTEXITCODE_SUCCESS;
411 }
412
413 case 'f':
414 {
415 fDaemonise = false;
416 fRespawn = false;
417 break;
418 }
419
420 case 'l':
421 {
422 rc = RTStrCopy(g_szLogFile, sizeof(g_szLogFile), ValueUnion.psz);
423 if (RT_FAILURE(rc))
424 return RTMsgErrorExitFailure("Unable to set log file path, rc=%Rrc\n", rc);
425 break;
426 }
427
428 case 'n':
429 {
430 fRespawn = false;
431 break;
432 }
433
434 case 'v':
435 {
436 g_cVerbosity++;
437 break;
438 }
439
440 case 'V':
441 {
442 RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
443 return RTEXITCODE_SUCCESS;
444 }
445
446 /* Services */
447#ifdef VBOX_WITH_GUEST_PROPS
448 case VBOXCLIENT_OPT_CHECKHOSTVERSION:
449 {
450 if (g_Service.pDesc)
451 return vbclSyntaxOnlyOneService();
452 g_Service.pDesc = &g_SvcHostVersion;
453 break;
454 }
455#endif
456#ifdef VBOX_WITH_SHARED_CLIPBOARD
457 case VBOXCLIENT_OPT_CLIPBOARD:
458 {
459 if (g_Service.pDesc)
460 return vbclSyntaxOnlyOneService();
461 g_Service.pDesc = &g_SvcClipboard;
462 break;
463 }
464#endif
465#ifdef VBOX_WITH_DRAG_AND_DROP
466 case VBOXCLIENT_OPT_DRAGANDDROP:
467 {
468 if (g_Service.pDesc)
469 return vbclSyntaxOnlyOneService();
470 g_Service.pDesc = &g_SvcDragAndDrop;
471 break;
472 }
473#endif
474#ifdef VBOX_WITH_SEAMLESS
475 case VBOXCLIENT_OPT_SEAMLESS:
476 {
477 if (g_Service.pDesc)
478 return vbclSyntaxOnlyOneService();
479 g_Service.pDesc = &g_SvcSeamless;
480 break;
481 }
482#endif
483#ifdef VBOX_WITH_VMSVGA
484 case VBOXCLIENT_OPT_VMSVGA:
485 {
486 if (g_Service.pDesc)
487 return vbclSyntaxOnlyOneService();
488 g_Service.pDesc = &g_SvcDisplaySVGA;
489 break;
490 }
491 case VBOXCLIENT_OPT_VMSVGA_SESSION:
492 {
493 if (g_Service.pDesc)
494 return vbclSyntaxOnlyOneService();
495 g_Service.pDesc = &g_SvcDisplaySVGASession;
496 break;
497 }
498#endif
499 case VINF_GETOPT_NOT_OPTION:
500 break;
501
502 case VERR_GETOPT_UNKNOWN_OPTION:
503 RT_FALL_THROUGH();
504 default:
505 {
506 if ( g_Service.pDesc
507 && g_Service.pDesc->pfnOption)
508 {
509 rc = g_Service.pDesc->pfnOption(NULL, argc, argv, &GetState.iNext);
510 }
511 else /* No service specified yet. */
512 rc = VERR_NOT_FOUND;
513
514 if (RT_FAILURE(rc))
515 {
516 RTMsgError("unrecognized option '%s'", ValueUnion.psz);
517 RTMsgInfo("Try '%s --help' for more information", pcszFileName);
518 return RTEXITCODE_SYNTAX;
519 }
520 break;
521 }
522
523 } /* switch */
524 } /* while RTGetOpt */
525
526 if (!g_Service.pDesc)
527 return RTMsgErrorExit(RTEXITCODE_SYNTAX, "No service specified. Quitting because nothing to do!");
528
529 /* Initialize VbglR3 before we do anything else with the logger. */
530 rc = VbglR3InitUser();
531 if (RT_FAILURE(rc))
532 return RTMsgErrorExitFailure("VbglR3InitUser failed: %Rrc", rc);
533
534 rc = VBClLogCreate(g_szLogFile[0] ? g_szLogFile : "");
535 if (RT_FAILURE(rc))
536 return RTMsgErrorExitFailure("Failed to create release log '%s', rc=%Rrc\n",
537 g_szLogFile[0] ? g_szLogFile : "<None>", rc);
538
539 if (!fDaemonise)
540 {
541 /* If the user is running in "no daemon" mode, send critical logging to stdout as well. */
542 PRTLOGGER pReleaseLog = RTLogRelGetDefaultInstance();
543 if (pReleaseLog)
544 {
545 rc = RTLogDestinations(pReleaseLog, "stdout");
546 if (RT_FAILURE(rc))
547 return RTMsgErrorExitFailure("Failed to redivert error output, rc=%Rrc", rc);
548 }
549 }
550
551 VBClLogInfo("VBoxClient %s r%s started. Verbose level = %d\n", RTBldCfgVersion(), RTBldCfgRevisionStr(), g_cVerbosity);
552 VBClLogInfo("Service: %s\n", g_Service.pDesc->pszDesc);
553
554 rc = RTCritSectInit(&g_critSect);
555 if (RT_FAILURE(rc))
556 VBClLogFatalError("Initializing critical section failed: %Rrc\n", rc);
557 if (g_Service.pDesc->pszPidFilePath)
558 {
559 rc = RTPathUserHome(g_szPidFile, sizeof(g_szPidFile));
560 if (RT_FAILURE(rc))
561 VBClLogFatalError("Getting home directory failed: %Rrc\n", rc);
562 rc = RTPathAppend(g_szPidFile, sizeof(g_szPidFile), g_Service.pDesc->pszPidFilePath);
563 if (RT_FAILURE(rc))
564 VBClLogFatalError("Creating PID file path failed: %Rrc\n", rc);
565 }
566
567 if (fDaemonise)
568 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */, fRespawn, &g_cRespawn);
569 if (RT_FAILURE(rc))
570 VBClLogFatalError("Daemonizing service failed: %Rrc\n", rc);
571
572 if (g_szPidFile[0])
573 {
574 rc = VbglR3PidFile(g_szPidFile, &g_hPidFile);
575 if (rc == VERR_FILE_LOCK_VIOLATION) /* Already running. */
576 return RTEXITCODE_SUCCESS;
577 if (RT_FAILURE(rc))
578 VBClLogFatalError("Creating PID file failed: %Rrc\n", rc);
579 }
580
581#ifndef VBOXCLIENT_WITHOUT_X11
582 /* Set an X11 error handler, so that we don't die when we get unavoidable
583 * errors. */
584 XSetErrorHandler(vboxClientXLibErrorHandler);
585 /* Set an X11 I/O error handler, so that we can shutdown properly on
586 * fatal errors. */
587 XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
588#endif
589
590 bool fSignalHandlerInstalled = false;
591 if (RT_SUCCESS(rc))
592 {
593 rc = vboxClientSignalHandlerInstall();
594 if (RT_SUCCESS(rc))
595 fSignalHandlerInstalled = true;
596 }
597
598 if ( RT_SUCCESS(rc)
599 && g_Service.pDesc->pfnInit)
600 {
601 VBClLogInfo("Initializing service ...\n");
602 rc = g_Service.pDesc->pfnInit();
603 }
604
605 if (RT_SUCCESS(rc))
606 {
607 VBClLogInfo("Creating worker thread ...\n");
608 rc = RTThreadCreate(&g_Service.Thread, vbclThread, (void *)&g_Service, 0,
609 RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_Service.pDesc->pszName);
610 if (RT_FAILURE(rc))
611 {
612 VBClLogError("Creating worker thread failed, rc=%Rrc\n", rc);
613 }
614 else
615 {
616 g_Service.fStarted = true;
617
618 /* Wait for the thread to initialize. */
619 /** @todo There is a race between waiting and checking
620 * the fShutdown flag of a thread here and processing
621 * the thread's actual worker loop. If the thread decides
622 * to exit the loop before we skipped the fShutdown check
623 * below the service will fail to start! */
624 /** @todo This presumably means either a one-shot service or that
625 * something has gone wrong. In the second case treating it as failure
626 * to start is probably right, so we need a way to signal the first
627 * rather than leaving the idle thread hanging around. A flag in the
628 * service description? */
629 RTThreadUserWait(g_Service.Thread, RT_MS_1MIN);
630 if (g_Service.fShutdown)
631 {
632 VBClLogError("Service failed to start!\n");
633 rc = VERR_GENERAL_FAILURE;
634 }
635 else
636 {
637 VBClLogInfo("Service started\n");
638
639 int rcThread;
640 rc = RTThreadWait(g_Service.Thread, RT_INDEFINITE_WAIT, &rcThread);
641 if (RT_SUCCESS(rc))
642 rc = rcThread;
643
644 if (RT_FAILURE(rc))
645 VBClLogError("Waiting on worker thread to stop failed, rc=%Rrc\n", rc);
646
647 if (g_Service.pDesc->pfnTerm)
648 {
649 VBClLogInfo("Terminating service\n");
650
651 int rc2 = g_Service.pDesc->pfnTerm();
652 if (RT_SUCCESS(rc))
653 rc = rc2;
654
655 if (RT_SUCCESS(rc))
656 {
657 VBClLogInfo("Service terminated\n");
658 }
659 else
660 VBClLogError("Service failed to terminate, rc=%Rrc\n", rc);
661 }
662 }
663 }
664 }
665
666 if (RT_FAILURE(rc))
667 {
668 if (rc == VERR_NOT_AVAILABLE)
669 VBClLogInfo("Service is not availabe, skipping\n");
670 else if (rc == VERR_NOT_SUPPORTED)
671 VBClLogInfo("Service is not supported on this platform, skipping\n");
672 else
673 VBClLogError("Service ended with error %Rrc\n", rc);
674 }
675 else
676 VBClLogVerbose(2, "Service ended\n");
677
678 if (fSignalHandlerInstalled)
679 {
680 int rc2 = vboxClientSignalHandlerUninstall();
681 AssertRC(rc2);
682 }
683
684 VBClShutdown(false /*fExit*/);
685
686 /** @todo r=andy Should we return an appropriate exit code if the service failed to init?
687 * Must be tested carefully with our init scripts first. */
688 return RTEXITCODE_SUCCESS;
689}
690
Note: See TracBrowser for help on using the repository browser.

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