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 (GPL) as published by the Free Software
|
---|
14 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
15 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
16 | * hope that it will 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 | #include <sys/types.h>
|
---|
28 | #include <stdlib.h> /* For exit */
|
---|
29 | #include <unistd.h>
|
---|
30 | #include <getopt.h>
|
---|
31 |
|
---|
32 | #include <X11/Xlib.h>
|
---|
33 | #include <X11/Intrinsic.h>
|
---|
34 |
|
---|
35 | #include "clipboard.h"
|
---|
36 |
|
---|
37 | #ifdef SEAMLESS_X11
|
---|
38 | # include "seamless.h"
|
---|
39 | #endif
|
---|
40 |
|
---|
41 | static bool gbDaemonise = true;
|
---|
42 |
|
---|
43 | /**
|
---|
44 | * Xlib error handler for certain errors that we can't avoid.
|
---|
45 | */
|
---|
46 | int vboxClientXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
|
---|
47 | {
|
---|
48 | char errorText[1024];
|
---|
49 |
|
---|
50 | if (pError->error_code == BadAtom)
|
---|
51 | {
|
---|
52 | /* This can be triggered in debug builds if a guest application passes a bad atom
|
---|
53 | in its list of supported clipboard formats. As such it is harmless. */
|
---|
54 | Log(("VBoxService: ignoring BadAtom error and returning\n"));
|
---|
55 | return 0;
|
---|
56 | }
|
---|
57 | if (pError->error_code == BadWindow)
|
---|
58 | {
|
---|
59 | /* This can be triggered if a guest application destroys a window before we notice. */
|
---|
60 | Log(("VBoxService: ignoring BadWindow error and returning\n"));
|
---|
61 | return 0;
|
---|
62 | }
|
---|
63 | #ifdef VBOX_X11_CLIPBOARD
|
---|
64 | vboxClipboardDisconnect();
|
---|
65 | #endif
|
---|
66 | XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
|
---|
67 | LogRel(("VBoxService: an X Window protocol error occurred: %s. Request code: %d, minor code: %d, serial number: %d\n",
|
---|
68 | pError->error_code, pError->request_code, pError->minor_code, pError->serial));
|
---|
69 | exit(1);
|
---|
70 | }
|
---|
71 |
|
---|
72 | int main(int argc, char *argv[])
|
---|
73 | {
|
---|
74 | int rc;
|
---|
75 | #ifdef SEAMLESS_X11
|
---|
76 | /** Our instance of the seamless class. */
|
---|
77 | VBoxGuestSeamless seamless;
|
---|
78 | #endif
|
---|
79 |
|
---|
80 | /* Parse our option(s) */
|
---|
81 | /** @todo r=bird: use RTGetOpt */
|
---|
82 | while (1)
|
---|
83 | {
|
---|
84 | static struct option sOpts[] =
|
---|
85 | {
|
---|
86 | {"nodaemon", 0, 0, 'd'},
|
---|
87 | {0, 0, 0, 0}
|
---|
88 | };
|
---|
89 | int cOpt = getopt_long(argc, argv, "", sOpts, 0);
|
---|
90 | if (cOpt == -1)
|
---|
91 | {
|
---|
92 | if (optind < argc)
|
---|
93 | {
|
---|
94 | std::cout << "Unrecognized command line argument: " << argv[argc] << std::endl;
|
---|
95 | exit(1);
|
---|
96 | }
|
---|
97 | break;
|
---|
98 | }
|
---|
99 | switch(cOpt)
|
---|
100 | {
|
---|
101 | case 'd':
|
---|
102 | gbDaemonise = false;
|
---|
103 | break;
|
---|
104 | default:
|
---|
105 | std::cout << "Unrecognized command line option: " << static_cast<char>(cOpt)
|
---|
106 | << std::endl;
|
---|
107 | case '?':
|
---|
108 | exit(1);
|
---|
109 | }
|
---|
110 | }
|
---|
111 | if (gbDaemonise)
|
---|
112 | {
|
---|
113 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
|
---|
114 | if (RT_FAILURE(rc))
|
---|
115 | {
|
---|
116 | LogRel(("VBoxService: failed to daemonize. exiting."));
|
---|
117 | return 1;
|
---|
118 | }
|
---|
119 | }
|
---|
120 | /* Initialise our runtime before all else. */
|
---|
121 | RTR3Init(false);
|
---|
122 | rc = VbglR3Init();
|
---|
123 | if (RT_FAILURE(rc))
|
---|
124 | {
|
---|
125 | std::cout << "Failed to connect to the VirtualBox kernel service" << std::endl;
|
---|
126 | return 1;
|
---|
127 | }
|
---|
128 | LogRel(("VBoxService: starting...\n"));
|
---|
129 | /* Initialise threading in X11 and in Xt. */
|
---|
130 | if (!XInitThreads() || !XtToolkitThreadInitialize())
|
---|
131 | {
|
---|
132 | LogRel(("VBoxService: error initialising threads in X11, exiting."));
|
---|
133 | return 1;
|
---|
134 | }
|
---|
135 | /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
|
---|
136 | XSetErrorHandler(vboxClientXLibErrorHandler);
|
---|
137 | #ifdef VBOX_X11_CLIPBOARD
|
---|
138 | /* Connect to the host clipboard. */
|
---|
139 | LogRel(("VBoxService: starting clipboard Guest Additions...\n"));
|
---|
140 | rc = vboxClipboardConnect();
|
---|
141 | if (RT_SUCCESS(rc))
|
---|
142 | {
|
---|
143 | LogRel(("VBoxService: vboxClipboardConnect failed with rc = %Rrc\n", rc));
|
---|
144 | }
|
---|
145 | #endif /* VBOX_X11_CLIPBOARD defined */
|
---|
146 | #ifdef SEAMLESS_X11
|
---|
147 | try
|
---|
148 | {
|
---|
149 | LogRel(("VBoxService: starting seamless Guest Additions...\n"));
|
---|
150 | rc = seamless.init();
|
---|
151 | if (RT_FAILURE(rc))
|
---|
152 | {
|
---|
153 | LogRel(("VBoxService: failed to initialise seamless Additions, rc = %Rrc\n", rc));
|
---|
154 | }
|
---|
155 | }
|
---|
156 | catch (std::exception e)
|
---|
157 | {
|
---|
158 | LogRel(("VBoxService: failed to initialise seamless Additions - caught exception: %s\n", e.what()));
|
---|
159 | rc = VERR_UNRESOLVED_ERROR;
|
---|
160 | }
|
---|
161 | catch (...)
|
---|
162 | {
|
---|
163 | LogRel(("VBoxService: failed to initialise seamless Additions - caught unknown exception.\n"));
|
---|
164 | rc = VERR_UNRESOLVED_ERROR;
|
---|
165 | }
|
---|
166 | #endif /* SEAMLESS_X11 defined */
|
---|
167 | #ifdef VBOX_X11_CLIPBOARD
|
---|
168 | LogRel(("VBoxService: connecting to the shared clipboard service.\n"));
|
---|
169 | vboxClipboardMain();
|
---|
170 | vboxClipboardDisconnect();
|
---|
171 | #else /* VBOX_X11_CLIPBOARD not defined */
|
---|
172 | LogRel(("VBoxService: sleeping...\n"));
|
---|
173 | pause();
|
---|
174 | LogRel(("VBoxService: exiting...\n"));
|
---|
175 | #endif /* VBOX_X11_CLIPBOARD not defined */
|
---|
176 | #ifdef SEAMLESS_X11
|
---|
177 | try
|
---|
178 | {
|
---|
179 | seamless.uninit();
|
---|
180 | }
|
---|
181 | catch (std::exception e)
|
---|
182 | {
|
---|
183 | LogRel(("VBoxService: error shutting down seamless Additions - caught exception: %s\n", e.what()));
|
---|
184 | rc = VERR_UNRESOLVED_ERROR;
|
---|
185 | }
|
---|
186 | catch (...)
|
---|
187 | {
|
---|
188 | LogRel(("VBoxService: error shutting down seamless Additions - caught unknown exception.\n"));
|
---|
189 | rc = VERR_UNRESOLVED_ERROR;
|
---|
190 | }
|
---|
191 | #endif /* SEAMLESS_X11 defined */
|
---|
192 | return rc;
|
---|
193 | }
|
---|