main.cpp revision 5da2719fd287c91bfab2269c8cede652f055af2b
/** @file
*
* VirtualBox Guest Service:
* Linux guest.
*/
/*
* Copyright (C) 2006-2011 Oracle Corporation
*
* This file is part of VirtualBox Open Source Edition (OSE), as
* available from http://www.virtualbox.org. This file is free software;
* General Public License (GPL) as published by the Free Software
* Foundation, in version 2 as it comes in the "COPYING" file of the
* VirtualBox OSE distribution. VirtualBox OSE is distributed in the
* hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
*/
#include <stdlib.h> /* For exit */
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>
#include <poll.h>
#include <signal.h>
#include <iprt/critsect.h>
#include <iprt/initterm.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxClient.h"
/** Object representing the service we are running. This has to be global
* so that the cleanup routine can access it. */
struct VBCLSERVICE **g_pService;
/** 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).
*/
/** Exit with a fatal error. */
void vbclFatalError(char *pszMessage)
{
char *pszCommand;
if (pszMessage)
{
if (pszCommand)
}
_exit(1);
}
/** Clean up if we get a signal or something. This is extern so that we
* can call it from other compilation units. */
void VBClCleanUp()
{
/* 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. */
if (RT_FAILURE(rc))
if (g_pService)
if (g_szPidFile[0] && g_hPidFile)
VbglR3Term();
exit(0);
}
/**
* A standard signal handler which cleans up and exits.
*/
static void vboxClientSignalHandler(int cSignal)
{
/** Disable seamless mode */
RTPrintf(("VBoxClient: terminating...\n"));
VBClCleanUp();
}
/**
* Xlib error handler for certain errors that we can't avoid.
*/
{
char errorText[1024];
LogRelFlow(("VBoxClient: 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; /* We should never reach this. */
}
/**
* Xlib error handler for fatal errors. This often means that the programme is still running
* when X exits.
*/
{
LogRel(("VBoxClient: 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"));
VBClCleanUp();
return 0; /* We should never reach this. */
}
/**
* Reset all standard termination signals to call our signal handler, which
* cleans up and exits.
*/
static void vboxClientSetSignalHandlers(void)
{
LogRelFlowFunc(("\n"));
LogRelFlowFunc(("returning\n"));
}
/** Connect to the X server and return the "XFree86_VT" root window property,
* or 0 on failure. */
{
int actualFormat;
unsigned long *pValue;
(unsigned char **)&pValue);
{
}
return cVT;
}
/** Check whether the current virtual terminal is the one running the X server.
*/
{
int rc;
const char *pcszStage;
do {
if (RT_FAILURE(rc))
break;
pcszStage = "getting VT number from sysfs file";
if (RT_FAILURE(rc))
break;
pcszStage = "entering critical section";
if (RT_FAILURE(rc))
break;
pcszStage = "asking service to pause or resume";
else
if (RT_FAILURE(rc))
break;
pcszStage = "leaving critical section";
} while(false);
if (RT_FAILURE(rc))
{
LogRelFunc(("VBoxClient: failed at stage: \"%s\" rc: %Rrc cVT: %d szTTY: %s.\n",
if (RTCritSectIsOwner(&g_critSect))
VBClCleanUp();
}
}
/** Poll for TTY changes using sysfs and for X server disconnection.
* returns the currently active TTY as a string of the form "tty<n>", with n
* greater than zero. Polling for POLLPRI returns when the TTY changes.
* @a cVT should be zero if we do not know the X server's VT. */
{
unsigned cPollFD = 1;
int rc;
/* This block could be Linux-only, but keeping it on Solaris too, where it
* should just fail gracefully, gives us more code path coverage. */
if (cVT)
{
if (RT_SUCCESS(rc))
{
cPollFD = 2;
}
}
while (true)
{
if (hFile != NIL_RTFILE)
/* The only point of this loop is to trigger the I/O error handler if
* appropriate. */
{
}
/* If we get caught in a tight loop for some reason try to limit the
* damage. */
{
LogRel(("Monitor thread: unexpectedly fast event, revents=0x%x, 0x%x.\n",
}
{
LogRel(("Monitor thread: poll failed, stopping.\n"));
VBClCleanUp();
}
}
}
/**
* Thread which notifies the service when we switch to a different VT or back
* and cleans up when the X server exits.
* @note runs until programme exit.
*/
{
unsigned long cVT;
if (!pDisplay)
return VINF_SUCCESS;
/* Note: cVT will be 0 if we failed to get it. This is valid. */
/* Should never get here. */
return VINF_SUCCESS;
}
/**
* Start the thread which notifies the service when we switch to a different
* VT or back, and terminates us when the X server exits. The first is best
* effort functionality: XFree86 4.3 and older do not report their VT via the
* "XFree86_VT" root window property at all, and pre-2.6.38 Linux does not
* provide the interface in "sysfs" which we use. If there is a need for this
* to work with pre-2.6.38 Linux we can send the VT_GETSTATE ioctl to
*/
static int startMonitorThread()
{
RTTHREADTYPE_INFREQUENT_POLLER, 0, "MONITOR");
}
/**
* Print out a usage message and exit with success.
*/
void vboxClientUsage(const char *pcszFileName)
{
RTPrintf("Usage: %s --clipboard|"
#ifdef VBOX_WITH_DRAG_AND_DROP
"--draganddrop|"
#endif
"--display|"
# ifdef VBOX_WITH_GUEST_PROPS
"--checkhostversion|"
#endif
"--seamless [-d|--nodaemon]\n", pcszFileName);
RTPrintf("Start the VirtualBox X Window System guest services.\n\n");
RTPrintf("Options:\n");
RTPrintf(" --clipboard start the shared clipboard service\n");
#ifdef VBOX_WITH_DRAG_AND_DROP
RTPrintf(" --draganddrop start the drag and drop service\n");
#endif
RTPrintf(" --display start the display management service\n");
#ifdef VBOX_WITH_GUEST_PROPS
RTPrintf(" --checkhostversion start the host version notifier service\n");
#endif
RTPrintf(" --seamless start the seamless windows service\n");
RTPrintf(" -d, --nodaemon continue running as a system service\n");
RTPrintf("\n");
exit(0);
}
/**
* The main loop for the VBoxClient daemon.
* @todo Clean up for readability.
*/
{
bool fDaemonise = true;
int rc;
const char *pcszFileName, *pcszStage;
/* Initialise our runtime before all else. */
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())
VBClFatalError(("Failed to initialize X11 threads\n"));
/* Get our file name for error output. */
if (!pcszFileName)
pcszFileName = "VBoxClient";
/* Initialise the guest library. */
rc = VbglR3InitUser();
if (RT_FAILURE(rc))
VBClFatalError(("%s: failed to connect to the VirtualBox kernel service, rc=%Rrc\n",
pcszFileName, rc));
/* Parse our option(s) */
/** @todo Use RTGetOpt() if the arguments become more complex. */
for (int i = 1; i < argc; ++i)
{
{
/* If the user is running in "no daemon" mode anyway, send critical
* logging to stdout as well. */
if (pReleaseLog)
RTPrintf("%s: failed to redivert error output, rc=%Rrc\n",
pcszFileName, rc);
fDaemonise = false;
}
{
if (g_pService)
break;
}
{
if (g_pService)
break;
}
{
if (g_pService)
break;
}
{
if (g_pService)
break;
}
#ifdef VBOX_WITH_DRAG_AND_DROP
{
if (g_pService)
break;
}
#endif /* VBOX_WITH_DRAG_AND_DROP */
{
return 0;
}
else
{
return 1;
}
rc = VINF_SUCCESS;
}
{
return 1;
}
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
if (RT_FAILURE(rc))
(*g_pService)->getPidFilePath());
if (RT_FAILURE(rc))
if (fDaemonise)
if (RT_FAILURE(rc))
if (g_szPidFile[0])
if (RT_FAILURE(rc))
/* Set signal handlers to clean up on exit. */
/* Set an X11 error handler, so that we don't die when we get unavoidable
* errors. */
/* Set an X11 I/O error handler, so that we can shutdown properly on
* fatal errors. */
if (RT_FAILURE(rc))
rc = startMonitorThread();
if (RT_FAILURE(rc))
VBClCleanUp();
return 1;
}