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