VirtualBox

source: vbox/trunk/src/VBox/Additions/linux/xclient/main.cpp@ 4485

Last change on this file since 4485 was 4353, checked in by vboxsync, 17 years ago

export

File size: 4.4 KB
Line 
1/** @file
2 *
3 * VirtualBox Guest Service:
4 * Linux guest.
5 */
6
7/*
8 * Copyright (C) 2006-2007 innotek GmbH
9 *
10 * This file is part of VirtualBox Open Source Edition (OSE), as
11 * available from http://www.virtualbox.org. This file is free software;
12 * you can redistribute it and/or modify it under the terms of the GNU
13 * General Public License as published by the Free Software Foundation,
14 * in version 2 as it comes in the "COPYING" file of the VirtualBox OSE
15 * distribution. VirtualBox OSE is distributed in the hope that it will
16 * be useful, but WITHOUT ANY WARRANTY of any kind.
17 */
18
19#define LOG_GROUP LOG_GROUP_DEV_VMM_BACKDOOR
20
21#include <VBox/VBoxGuest.h>
22#include <VBox/log.h>
23#include <iprt/initterm.h>
24
25#include <iostream>
26
27using std::cout;
28using std::endl;
29
30#include <sys/types.h>
31#include <unistd.h>
32#include <getopt.h>
33
34#include <X11/Xlib.h>
35#include <X11/Intrinsic.h>
36
37#include "clipboard.h"
38
39static bool gbDaemonise = true;
40
41/**
42 * Become a daemon process
43 */
44void vboxDaemonise(void)
45{
46 /* First fork and exit the parent process, so that we are sure we are not session leader. */
47 if (fork() != 0)
48 {
49 exit(0);
50 }
51 /* Detach from the controlling terminal by creating our own session. */
52 setsid();
53 /* And change to the root directory to avoid holding the one we were started in open. */
54 chdir("/");
55 /* Close the standard files. */
56 close(0);
57 close(1);
58 close(2);
59}
60
61/**
62 * Xlib error handler, so that we don't abort when we get a BadAtom error.
63 */
64int vboxClipboardXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
65{
66 char errorText[1024];
67
68 LogFlowFunc(("\n"));
69 if (pError->error_code == BadAtom)
70 {
71 /* This can be triggered in debug builds if a guest application passes a bad atom
72 in its list of supported clipboard formats. As such it is harmless. */
73 LogFlowFunc(("ignoring BadAtom error and returning\n"));
74 return 0;
75 }
76 vboxClipboardDisconnect();
77 XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
78 if (!gbDaemonise)
79 {
80 cout << "An X Window protocol error occurred: " << errorText << endl
81 << " Request code: " << int(pError->request_code) << endl
82 << " Minor code: " << int(pError->minor_code) << endl
83 << " Serial number of the failed request: " << int(pError->serial) << endl;
84 }
85 Log(("%s: an X Window protocol error occurred: %s. Request code: %d, minor code: %d, serial number: %d\n",
86 __PRETTY_FUNCTION__, pError->error_code, pError->request_code, pError->minor_code,
87 pError->serial));
88 LogFlowFunc(("exiting\n"));
89 exit(1);
90}
91
92int main(int argc, char *argv[])
93{
94 int rc;
95
96 /* Parse our option(s) */
97 while (1)
98 {
99 static struct option sOpts[] =
100 {
101 {"nodaemon", 0, 0, 'd'},
102 {0, 0, 0, 0}
103 };
104 int cOpt = getopt_long(argc, argv, "", sOpts, 0);
105 if (cOpt == -1)
106 {
107 if (optind < argc)
108 {
109 cout << "Unrecognized command line argument: " << argv[argc] << endl;
110 exit(1);
111 }
112 break;
113 }
114 switch(cOpt)
115 {
116 case 'd':
117 gbDaemonise = false;
118 break;
119 default:
120 cout << "Unrecognized command line option: " << static_cast<char>(cOpt) << endl;
121 case '?':
122 exit(1);
123 }
124 }
125 /* Initialise our runtime before all else. */
126 RTR3Init(false);
127 LogFlowFunc(("\n"));
128 /* Initialise threading in X11 and in Xt. */
129 if (!XInitThreads() || !XtToolkitThreadInitialize())
130 {
131 LogRelFunc(("Error initialising threads in X11, returning 1."));
132 cout << "Your guest system appears to be using an old, single-threaded version of the X Window System libraries. This program cannot continue." << endl;
133 return 1;
134 }
135 /* Set an X11 error handler, so that we don't die when we get BadAtom errors. */
136 XSetErrorHandler(vboxClipboardXLibErrorHandler);
137 /* Connect to the host clipboard. */
138 rc = vboxClipboardConnect();
139 if (rc != VINF_SUCCESS)
140 {
141 Log(("vboxClipboardConnect failed with rc = %d\n", rc));
142 cout << "Failed to connect to the host clipboard." << endl;
143 LogFlowFunc(("returning 1\n"));
144 return 1;
145 }
146 if (gbDaemonise)
147 {
148 vboxDaemonise();
149 }
150 vboxClipboardMain();
151 vboxClipboardDisconnect();
152 LogFlowFunc(("returning 0\n"));
153 return 0;
154}
Note: See TracBrowser for help on using the repository browser.

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