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 |
|
---|
27 | using std::cout;
|
---|
28 | using 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 |
|
---|
39 | #ifdef SEAMLESS_LINUX
|
---|
40 | # include "seamless.h"
|
---|
41 | #endif
|
---|
42 |
|
---|
43 | static bool gbDaemonise = true;
|
---|
44 |
|
---|
45 | /**
|
---|
46 | * Become a daemon process
|
---|
47 | */
|
---|
48 | void vboxDaemonise(void)
|
---|
49 | {
|
---|
50 | /* First fork and exit the parent process, so that we are sure we are not session leader. */
|
---|
51 | if (fork() != 0)
|
---|
52 | {
|
---|
53 | exit(0);
|
---|
54 | }
|
---|
55 | /* Detach from the controlling terminal by creating our own session. */
|
---|
56 | setsid();
|
---|
57 | /* And change to the root directory to avoid holding the one we were started in open. */
|
---|
58 | chdir("/");
|
---|
59 | /* Close the standard files. */
|
---|
60 | close(0);
|
---|
61 | close(1);
|
---|
62 | close(2);
|
---|
63 | }
|
---|
64 |
|
---|
65 | /**
|
---|
66 | * Xlib error handler, so that we don't abort when we get a BadAtom error.
|
---|
67 | */
|
---|
68 | int vboxClipboardXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
|
---|
69 | {
|
---|
70 | char errorText[1024];
|
---|
71 |
|
---|
72 | LogFlowFunc(("\n"));
|
---|
73 | if (pError->error_code == BadAtom)
|
---|
74 | {
|
---|
75 | /* This can be triggered in debug builds if a guest application passes a bad atom
|
---|
76 | in its list of supported clipboard formats. As such it is harmless. */
|
---|
77 | LogFlowFunc(("ignoring BadAtom error and returning\n"));
|
---|
78 | return 0;
|
---|
79 | }
|
---|
80 | vboxClipboardDisconnect();
|
---|
81 | XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
|
---|
82 | if (!gbDaemonise)
|
---|
83 | {
|
---|
84 | cout << "An X Window protocol error occurred: " << errorText << endl
|
---|
85 | << " Request code: " << int(pError->request_code) << endl
|
---|
86 | << " Minor code: " << int(pError->minor_code) << endl
|
---|
87 | << " Serial number of the failed request: " << int(pError->serial) << endl;
|
---|
88 | }
|
---|
89 | Log(("%s: an X Window protocol error occurred: %s. Request code: %d, minor code: %d, serial number: %d\n",
|
---|
90 | __PRETTY_FUNCTION__, pError->error_code, pError->request_code, pError->minor_code,
|
---|
91 | pError->serial));
|
---|
92 | LogFlowFunc(("exiting\n"));
|
---|
93 | exit(1);
|
---|
94 | }
|
---|
95 |
|
---|
96 | int main(int argc, char *argv[])
|
---|
97 | {
|
---|
98 | int rc;
|
---|
99 | #ifdef SEAMLESS_LINUX
|
---|
100 | /** Our instance of the seamless class. */
|
---|
101 | VBoxGuestSeamless seamless;
|
---|
102 | #endif
|
---|
103 |
|
---|
104 | /* Parse our option(s) */
|
---|
105 | while (1)
|
---|
106 | {
|
---|
107 | static struct option sOpts[] =
|
---|
108 | {
|
---|
109 | {"nodaemon", 0, 0, 'd'},
|
---|
110 | {0, 0, 0, 0}
|
---|
111 | };
|
---|
112 | int cOpt = getopt_long(argc, argv, "", sOpts, 0);
|
---|
113 | if (cOpt == -1)
|
---|
114 | {
|
---|
115 | if (optind < argc)
|
---|
116 | {
|
---|
117 | cout << "Unrecognized command line argument: " << argv[argc] << endl;
|
---|
118 | exit(1);
|
---|
119 | }
|
---|
120 | break;
|
---|
121 | }
|
---|
122 | switch(cOpt)
|
---|
123 | {
|
---|
124 | case 'd':
|
---|
125 | gbDaemonise = false;
|
---|
126 | break;
|
---|
127 | default:
|
---|
128 | cout << "Unrecognized command line option: " << static_cast<char>(cOpt) << endl;
|
---|
129 | case '?':
|
---|
130 | exit(1);
|
---|
131 | }
|
---|
132 | }
|
---|
133 | /* Initialise our runtime before all else. */
|
---|
134 | RTR3Init(false);
|
---|
135 | LogFlowFunc(("\n"));
|
---|
136 | /* Initialise threading in X11 and in Xt. */
|
---|
137 | if (!XInitThreads() || !XtToolkitThreadInitialize())
|
---|
138 | {
|
---|
139 | LogRelFunc(("Error initialising threads in X11, returning 1."));
|
---|
140 | cout << "Your guest system appears to be using an old, single-threaded version of the X Window System libraries. This program cannot continue." << endl;
|
---|
141 | return 1;
|
---|
142 | }
|
---|
143 | /* Set an X11 error handler, so that we don't die when we get BadAtom errors. */
|
---|
144 | XSetErrorHandler(vboxClipboardXLibErrorHandler);
|
---|
145 | /* Connect to the host clipboard. */
|
---|
146 | LogRel(("Starting clipboard Guest Additions...\n"));
|
---|
147 | rc = vboxClipboardConnect();
|
---|
148 | if (rc != VINF_SUCCESS)
|
---|
149 | {
|
---|
150 | LogRel(("vboxClipboardConnect failed with rc = %d\n", rc));
|
---|
151 | cout << "Failed to connect to the host clipboard." << endl;
|
---|
152 | }
|
---|
153 | #ifdef SEAMLESS_LINUX
|
---|
154 | try
|
---|
155 | {
|
---|
156 | LogRel(("Starting seamless Guest Additions...\n"));
|
---|
157 | rc = seamless.init();
|
---|
158 | if (rc != VINF_SUCCESS)
|
---|
159 | {
|
---|
160 | LogRel(("Failed to initialise seamless Additions, rc = %d\n", rc));
|
---|
161 | cout << "Failed to initialise seamless Additions." << endl;
|
---|
162 | }
|
---|
163 | }
|
---|
164 | catch (std::exception e)
|
---|
165 | {
|
---|
166 | LogRel(("Failed to initialise seamless Additions - caught exception: %s\n", e.what()));
|
---|
167 | cout << "Failed to initialise seamless Additions\n" << endl;
|
---|
168 | rc = VERR_UNRESOLVED_ERROR;
|
---|
169 | }
|
---|
170 | catch (...)
|
---|
171 | {
|
---|
172 | LogRel(("Failed to initialise seamless Additions - caught unknown exception.\n"));
|
---|
173 | cout << "Failed to initialise seamless Additions\n" << endl;
|
---|
174 | rc = VERR_UNRESOLVED_ERROR;
|
---|
175 | }
|
---|
176 | #endif /* SEAMLESS_LINUX defined */
|
---|
177 | if (gbDaemonise)
|
---|
178 | {
|
---|
179 | vboxDaemonise();
|
---|
180 | }
|
---|
181 | vboxClipboardMain();
|
---|
182 | vboxClipboardDisconnect();
|
---|
183 | #ifdef SEAMLESS_LINUX
|
---|
184 | try
|
---|
185 | {
|
---|
186 | seamless.uninit();
|
---|
187 | }
|
---|
188 | catch (std::exception e)
|
---|
189 | {
|
---|
190 | LogRel(("Error shutting down seamless Additions - caught exception: %s\n", e.what()));
|
---|
191 | rc = VERR_UNRESOLVED_ERROR;
|
---|
192 | }
|
---|
193 | catch (...)
|
---|
194 | {
|
---|
195 | LogRel(("Error shutting down seamless Additions - caught unknown exception.\n"));
|
---|
196 | rc = VERR_UNRESOLVED_ERROR;
|
---|
197 | }
|
---|
198 | #endif /* SEAMLESS_LINUX defined */
|
---|
199 | LogFlowFunc(("returning %Vrc\n", rc));
|
---|
200 | return rc;
|
---|
201 | }
|
---|