main.cpp revision 417b0d3fbf32597b0d4d513c79498d02fe903924
/** @file
*
* VirtualBox Guest Service:
* Linux guest.
*/
/*
* Copyright (C) 2006-2010 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 <signal.h>
#include <iprt/critsect.h>
#include <iprt/initterm.h>
#include <VBox/VBoxGuestLib.h>
#include "VBoxClient.h"
#define TRACE RTPrintf("%s: %d\n", __PRETTY_FUNCTION__, __LINE__); LogRel(("%s: %d\n", __PRETTY_FUNCTION__, __LINE__))
/** Object representing the service we are running. This has to be global
* so that the cleanup routine can access it. */
/** 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 used to protect the clean-up routine, which can be
* called from different threads.
*/
/** Clean up if we get a signal or something. This is extern so that we
* can call it from other compilation units. */
void VBoxClient::CleanUp()
{
/* We never release this, as we end up with a call to exit(3) which is not
* async-safe. Until we fix this application properly, we should be sure
* never to exit from anywhere except from this method. */
if (RT_FAILURE(rc))
{
abort();
}
if (g_pService)
g_pService->cleanup();
if (g_szPidFile[0] && g_hPidFile)
VbglR3Term();
exit(0);
}
/**
* A standard signal handler which cleans up and exits.
*/
void vboxClientSignalHandler(int cSignal)
{
/** Disable seamless mode */
RTPrintf(("VBoxClient: terminating...\n"));
VBoxClient::CleanUp();
}
/**
* 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"));
VBoxClient::CleanUp();
return 0; /* We should never reach this. */
}
/**
* Reset all standard termination signals to call our signal handler, which
* cleans up and exits.
*/
void vboxClientSetSignalHandlers(void)
{
LogRelFlowFunc(("\n"));
LogRelFlowFunc(("returning\n"));
}
/**
* Print out a usage message and exit with success.
*/
void vboxClientUsage(const char *pcszFileName)
{
RTPrintf("Usage: %s --clipboard|--display|--checkhostversion|--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");
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.
*/
{
int rcClipboard, rc;
bool fDaemonise = true;
/* Have any fatal errors occurred yet? */
bool fSuccess = true;
/* Do we know which service we wish to run? */
bool fHaveService = false;
if (NULL == pszFileName)
pszFileName = "VBoxClient";
/* Initialise our runtime before all else. */
if (RT_FAILURE(rc))
{
/* Of course, this should never happen. */
exit(1);
}
/* Initialise our global clean-up critical section */
if (RT_FAILURE(rc))
{
/* Of course, this should never happen. */
exit(1);
}
/* Parse our option(s) */
/** @todo Use RTGetOpt() if the arguments become more complex. */
for (int i = 1; i < argc; ++i)
{
fDaemonise = false;
{
if (g_pService == NULL)
else
fSuccess = false;
}
{
if (g_pService == NULL)
else
fSuccess = false;
}
{
if (g_pService == NULL)
else
fSuccess = false;
}
{
if (g_pService == NULL)
else
fSuccess = false;
}
{
exit(0);
}
else
{
exit(1);
}
}
if (!fSuccess || !g_pService)
{
exit(1);
}
/* Get the path for the pidfiles */
if (RT_FAILURE(rc))
{
return 1;
}
if (RT_FAILURE(rc))
{
return 1;
}
/* Initialise the guest library. */
if (RT_FAILURE(VbglR3InitUser()))
{
RTPrintf("Failed to connect to the VirtualBox kernel service\n");
LogRel(("Failed to connect to the VirtualBox kernel service\n"));
return 1;
}
{
RTPrintf("Failed to create a pidfile. Exiting.\n");
LogRel(("Failed to create a pidfile. Exiting.\n"));
VbglR3Term();
return 1;
}
if (fDaemonise)
{
if (RT_FAILURE(rc))
{
RTPrintf("VBoxClient: failed to daemonize. Exiting.\n");
LogRel(("VBoxClient: failed to daemonize. Exiting.\n"));
# ifdef DEBUG
# endif
return 1;
}
}
/* 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. */
VBoxClient::CleanUp();
return 1; /* We should never get here. */
}