main.cpp revision b72771e8c6ba3b3d9ebdd7977730325131ae0f98
/** @file
*
* VirtualBox Guest Service:
* Linux guest.
*/
/*
* Copyright (C) 2006-2007 Sun Microsystems, Inc.
*
* 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.
*
* Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa
* Clara, CA 95054 USA or visit http://www.sun.com if you need
* additional information or have any questions.
*/
#include <VBox/VBoxGuest.h>
#include <iprt/initterm.h>
#include <stdlib.h> /* For exit */
#include <stdio.h>
#include <unistd.h>
#include <errno.h>
#include <signal.h>
#include <X11/Intrinsic.h>
#include "clipboard.h"
#ifdef DYNAMIC_RESIZE
# include "displaychange.h"
# ifdef SEAMLESS_GUEST
# include "seamless.h"
# endif
#endif
#define TRACE RTPrintf("%s: %d\n", __PRETTY_FUNCTION__, __LINE__); Log(("%s: %d\n", __PRETTY_FUNCTION__, __LINE__))
/* Make these global so that the destructors are called if we make an "emergency exit",
i.e. a (handled) signal or an X11 error. */
#ifdef DYNAMIC_RESIZE
# ifdef SEAMLESS_GUEST
/** Our instance of the seamless class. This only makes sense if dynamic resizing
is enabled. */
# endif /* SEAMLESS_GUEST defined */
#endif /* DYNAMIC_RESIZE */
#ifdef VBOX_X11_CLIPBOARD
#endif
/**
* Drop the programmes privileges to the caller's.
* @returns IPRT status code
* @todo move this into the R3 guest library
*/
int vboxClientDropPrivileges(void)
{
int rc = VINF_SUCCESS;
LogFlowFunc(("\n"));
#ifdef _POSIX_SAVED_IDS
#else
#endif
if (rcSystem < 0)
{
}
return rc;
}
/**
* Xlib error handler for certain errors that we can't avoid.
*/
{
char errorText[1024];
{
/* This can be triggered in debug builds if a guest application passes a bad atom
in its list of supported clipboard formats. As such it is harmless. */
Log(("VBoxClient: ignoring BadAtom error and returning\n"));
return 0;
}
{
/* This can be triggered if a guest application destroys a window before we notice. */
Log(("VBoxClient: ignoring BadWindow error and returning\n"));
return 0;
}
LogRel(("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));
/** Disable seamless mode */
VbglR3SeamlessSetCap(false);
VbglR3Term();
exit(1);
}
/**
* Xlib error handler for fatal errors. This often means that the programme is still running
* when X exits.
*/
{
Log(("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"));
/** Disable seamless mode */
VbglR3SeamlessSetCap(false);
VbglR3Term();
return gpfnOldIOErrorHandler(pDisplay);
}
/**
* A standard signal handler which cleans up and exits. Our global static objects will
* be cleaned up properly as we exit using "exit".
*/
void vboxClientSignalHandler(int cSignal)
{
/** Disable seamless mode */
VbglR3SeamlessSetCap(false);
RTPrintf(("VBoxClient: terminating...\n"));
/* Our pause() call will now return and exit. */
}
/**
* Reset all standard termination signals to call our signal handler, which cleans up
* and exits.
*/
void vboxClientSetSignalHandlers(void)
{
LogFlowFunc(("\n"));
LogFlowFunc(("returning\n"));
}
/**
* Print out a usage message and exit with success.
*/
void vboxClientUsage(const char *pcszFileName)
{
RTPrintf("Start the VirtualBox X Window System guest services.\n\n");
RTPrintf("Options:\n");
RTPrintf(" -d, --nodaemon do not lower privileges and continue running as a system\n");
RTPrintf(" service\n");
RTPrintf("\n");
exit(0);
}
/**
* The main loop for the VBoxClient daemon.
*/
{
bool fDaemonise = true;
if (NULL == pszFileName)
pszFileName = "VBoxClient";
/* Initialise our runtime before all else. */
RTR3Init();
/* Parse our option(s) */
/** @todo Use RTGetOpt() if the arguments become more complex. */
for (int i = 1; i < argc; ++i)
{
fDaemonise = false;
{
exit(0);
}
else
{
exit(1);
}
}
if (fDaemonise)
{
NULL);
if (RT_FAILURE(rc))
{
RTPrintf("VBoxClient: failed to daemonize. exiting.\n");
#ifdef DEBUG
#endif
return 1;
}
}
/* Initialise the guest library. */
if (RT_FAILURE(VbglR3Init()))
{
RTPrintf("Failed to connect to the VirtualBox kernel service\n");
return 1;
}
return 1;
LogRel(("VBoxClient: starting...\n"));
/* Initialise threading in X11 and in Xt. */
if (!XInitThreads() || !XtToolkitThreadInitialize())
{
LogRel(("VBoxClient: error initialising threads in X11, exiting.\n"));
return 1;
}
/* 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. */
try
{
#ifdef VBOX_X11_CLIPBOARD
/* Connect to the host clipboard. */
LogRel(("VBoxClient: starting clipboard Guest Additions...\n"));
if (RT_FAILURE(rcClipboard))
{
}
#endif /* VBOX_X11_CLIPBOARD defined */
#ifdef DYNAMIC_RESIZE
LogRel(("VBoxClient: starting dynamic guest resizing...\n"));
if (RT_FAILURE(rc))
{
}
# ifdef SEAMLESS_GUEST
if (RT_SUCCESS(rc))
{
LogRel(("VBoxClient: starting seamless Guest Additions...\n"));
if (RT_FAILURE(rc))
{
}
}
# endif /* SEAMLESS_GUEST defined */
#endif /* DYNAMIC_RESIZE defined */
}
{
}
catch (...)
{
LogRel(("VBoxClient: failed to initialise Guest Additions - caught unknown exception.\n"));
}
LogRel(("VBoxClient: sleeping...\n"));
pause();
LogRel(("VBoxClient: exiting...\n"));
try
{
/* r=frank: Why all these 2s delays? What are we waiting for? */
#ifdef DYNAMIC_RESIZE
# ifdef SEAMLESS_GUEST
LogRel(("VBoxClient: shutting down seamless Guest Additions...\n"));
# endif /* SEAMLESS_GUEST defined */
LogRel(("VBoxClient: shutting down dynamic guest resizing...\n"));
#endif /* DYNAMIC_RESIZE defined */
#ifdef VBOX_X11_CLIPBOARD
/* Connect to the host clipboard. */
LogRel(("VBoxClient: shutting down clipboard Guest Additions...\n"));
#endif /* VBOX_X11_CLIPBOARD defined */
}
{
}
catch (...)
{
LogRel(("VBoxClient: failed to shut down Guest Additions - caught unknown exception.\n"));
}
VbglR3Term();
}