VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/xclient/main.cpp@ 7264

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

Additions/x11: some cleaning up in the VBoxClient module

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 9.6 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 (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#include <iprt/path.h>
25
26#include <iostream>
27#include <cstdio>
28
29#include <sys/types.h>
30#include <stdlib.h> /* For exit */
31#include <unistd.h>
32#include <errno.h>
33#include <signal.h>
34
35#include <X11/Xlib.h>
36#include <X11/Intrinsic.h>
37
38#include "clipboard.h"
39
40#ifdef DYNAMIC_RESIZE
41# include "displaychange.h"
42# ifdef SEAMLESS_GUEST
43# include "seamless.h"
44# endif
45#endif
46
47#define TRACE printf("%s: %d\n", __PRETTY_FUNCTION__, __LINE__); Log(("%s: %d\n", __PRETTY_FUNCTION__, __LINE__))
48
49static int (*gpfnOldIOErrorHandler)(Display *) = NULL;
50
51/* Make these global so that the destructors are called if we make an "emergency exit",
52 i.e. a (handled) signal or an X11 error. */
53#ifdef DYNAMIC_RESIZE
54VBoxGuestDisplayChangeMonitor gDisplayChange;
55# ifdef SEAMLESS_GUEST
56 /** Our instance of the seamless class. This only makes sense if dynamic resizing
57 is enabled. */
58 VBoxGuestSeamless gSeamless;
59# endif /* SEAMLESS_GUEST defined */
60#endif /* DYNAMIC_RESIZE */
61#ifdef VBOX_X11_CLIPBOARD
62 VBoxGuestClipboard gClipboard;
63#endif
64
65/**
66 * Drop the programmes privileges to the caller's.
67 * @returns IPRT status code
68 * @todo move this into the R3 guest library
69 */
70int vboxClientDropPrivileges(void)
71{
72 int rc = VINF_SUCCESS;
73 int rcSystem, rcErrno;
74
75#ifdef _POSIX_SAVED_IDS
76 rcSystem = setuid(getuid());
77#else
78 rcSystem = setreuid(-1, getuid());
79#endif
80 if (rcSystem < 0)
81 {
82 rcErrno = errno;
83 rc = RTErrConvertFromErrno(rcErrno);
84 LogRel(("VBoxClient: failed to drop privileges, error %Rrc.\n", rc));
85 }
86 return rc;
87}
88
89/**
90 * Xlib error handler for certain errors that we can't avoid.
91 */
92int vboxClientXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
93{
94 char errorText[1024];
95
96 if (pError->error_code == BadAtom)
97 {
98 /* This can be triggered in debug builds if a guest application passes a bad atom
99 in its list of supported clipboard formats. As such it is harmless. */
100 Log(("VBoxClient: ignoring BadAtom error and returning\n"));
101 return 0;
102 }
103 if (pError->error_code == BadWindow)
104 {
105 /* This can be triggered if a guest application destroys a window before we notice. */
106 Log(("VBoxClient: ignoring BadWindow error and returning\n"));
107 return 0;
108 }
109 XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
110 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));
111 VbglR3Term();
112 exit(1);
113}
114
115/**
116 * Xlib error handler for fatal errors. This often means that the programme is still running
117 * when X exits.
118 */
119int vboxClientXLibIOErrorHandler(Display *pDisplay)
120{
121 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"));
122 VbglR3Term();
123 return gpfnOldIOErrorHandler(pDisplay);
124}
125
126/**
127 * A standard signal handler which cleans up and exits. Our global static objects will
128 * be cleaned up properly as we exit using "exit".
129 */
130void vboxClientSignalHandler(int cSignal)
131{
132 Log(("VBoxClient: terminated with signal %d\n", cSignal));
133 /* Our pause() call will now return and exit. */
134}
135
136/**
137 * Reset all standard termination signals to call our signal handler, which cleans up
138 * and exits.
139 */
140void vboxClientSetSignalHandlers(void)
141{
142 struct sigaction sigAction = { { 0 } };
143
144 sigAction.sa_handler = vboxClientSignalHandler;
145 sigemptyset(&sigAction.sa_mask);
146 sigaction(SIGHUP, &sigAction, NULL);
147 sigaction(SIGINT, &sigAction, NULL);
148 sigaction(SIGQUIT, &sigAction, NULL);
149 sigaction(SIGABRT, &sigAction, NULL);
150 sigaction(SIGPIPE, &sigAction, NULL);
151 sigaction(SIGALRM, &sigAction, NULL);
152 sigaction(SIGTERM, &sigAction, NULL);
153 sigaction(SIGUSR1, &sigAction, NULL);
154 sigaction(SIGUSR2, &sigAction, NULL);
155}
156
157/**
158 * Print out a usage message and exit with success.
159 */
160void vboxClientUsage(const char *pcszFileName)
161{
162 /* printf is better for i18n than iostream. */
163 printf("Usage: %s [-d|--nodaemon]\n", pcszFileName);
164 printf("Start the VirtualBox X Window System guest services.\n\n");
165 printf("Options:\n");
166 printf(" -d, --nodaemon do not lower privileges and continue running as a system\n");
167 printf(" service\n");
168 exit(0);
169}
170
171/**
172 * The main loop for the VBoxClient daemon.
173 */
174int main(int argc, char *argv[])
175{
176 int rcClipboard, rc = VINF_SUCCESS;
177 const char *pszFileName = RTPathFilename(argv[0]);
178 bool fDaemonise = true;
179
180 if (NULL == pszFileName)
181 pszFileName = "VBoxClient";
182
183 TRACE;
184 /* Parse our option(s) */
185 /** @todo Use RTGetOpt() if the arguments become more complex. */
186 for (int i = 1; i < argc; ++i)
187 {
188 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--nodaemon"))
189 fDaemonise = false;
190 else if (!strcmp(argv[i], "-h") || strcmp(argv[i], "--help"))
191 {
192 vboxClientUsage(pszFileName);
193 exit(0);
194 }
195 else
196 {
197 /* printf is better than iostream for i18n. */
198 printf("%s: unrecognized option `%s'\n", pszFileName, argv[i]);
199 printf("Try `%s --help' for more information\n", pszFileName);
200 exit(1);
201 }
202 }
203 TRACE;
204 if (fDaemonise)
205 {
206 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
207 if (RT_FAILURE(rc))
208 {
209 std::cout << "VBoxClient: failed to daemonize. exiting."<< std::endl;
210 return 1;
211 }
212 }
213 /* Initialise our runtime before all else. */
214 TRACE;
215 RTR3Init(false);
216 if (RT_FAILURE(VbglR3Init()))
217 {
218 std::cout << "Failed to connect to the VirtualBox kernel service" << std::endl;
219 return 1;
220 }
221 TRACE;
222 if (fDaemonise && RT_FAILURE(vboxClientDropPrivileges()))
223 return 1;
224 LogRel(("VBoxClient: starting...\n"));
225 /* Initialise threading in X11 and in Xt. */
226 if (!XInitThreads() || !XtToolkitThreadInitialize())
227 {
228 LogRel(("VBoxClient: error initialising threads in X11, exiting."));
229 return 1;
230 }
231 /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
232 XSetErrorHandler(vboxClientXLibErrorHandler);
233 /* Set an X11 I/O error handler, so that we can shutdown properly on fatal errors. */
234 gpfnOldIOErrorHandler = XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
235 vboxClientSetSignalHandlers();
236 try
237 {
238#ifdef VBOX_X11_CLIPBOARD
239 /* Connect to the host clipboard. */
240 LogRel(("VBoxClient: starting clipboard Guest Additions...\n"));
241 rcClipboard = gClipboard.init();
242 if (RT_FAILURE(rcClipboard))
243 {
244 LogRel(("VBoxClient: vboxClipboardConnect failed with rc = %Rrc\n", rc));
245 }
246#endif /* VBOX_X11_CLIPBOARD defined */
247#ifdef DYNAMIC_RESIZE
248 LogRel(("VBoxClient: starting dynamic guest resizing...\n"));
249 rc = gDisplayChange.init();
250 if (RT_FAILURE(rc))
251 {
252 LogRel(("VBoxClient: failed to start dynamic guest resizing, rc = %Rrc\n", rc));
253 }
254# ifdef SEAMLESS_GUEST
255 if (RT_SUCCESS(rc))
256 {
257 LogRel(("VBoxClient: starting seamless Guest Additions...\n"));
258 rc = gSeamless.init();
259 if (RT_FAILURE(rc))
260 {
261 LogRel(("VBoxClient: failed to start seamless Additions, rc = %Rrc\n", rc));
262 }
263 }
264# endif /* SEAMLESS_GUEST defined */
265#endif /* DYNAMIC_RESIZE defined */
266 }
267 catch (std::exception e)
268 {
269 LogRel(("VBoxClient: failed to initialise Guest Additions - caught exception: %s\n", e.what()));
270 rc = VERR_UNRESOLVED_ERROR;
271 }
272 catch (...)
273 {
274 LogRel(("VBoxClient: failed to initialise Guest Additions - caught unknown exception.\n"));
275 rc = VERR_UNRESOLVED_ERROR;
276 }
277 LogRel(("VBoxClient: sleeping...\n"));
278 pause();
279 LogRel(("VBoxClient: exiting...\n"));
280 try
281 {
282#ifdef DYNAMIC_RESIZE
283# ifdef SEAMLESS_GUEST
284 LogRel(("VBoxClient: shutting down seamless Guest Additions...\n"));
285 gSeamless.uninit(2000);
286# endif /* SEAMLESS_GUEST defined */
287 LogRel(("VBoxClient: shutting down dynamic guest resizing...\n"));
288 gDisplayChange.uninit(2000);
289#endif /* DYNAMIC_RESIZE defined */
290#ifdef VBOX_X11_CLIPBOARD
291 /* Connect to the host clipboard. */
292 LogRel(("VBoxClient: shutting down clipboard Guest Additions...\n"));
293 gClipboard.uninit(2000);
294#endif /* VBOX_X11_CLIPBOARD defined */
295 }
296 catch (std::exception e)
297 {
298 LogRel(("VBoxClient: failed to shut down Guest Additions - caught exception: %s\n", e.what()));
299 rc = VERR_UNRESOLVED_ERROR;
300 }
301 catch (...)
302 {
303 LogRel(("VBoxClient: failed to shut down Guest Additions - caught unknown exception.\n"));
304 rc = VERR_UNRESOLVED_ERROR;
305 }
306 VbglR3Term();
307 return RT_SUCCESS(rc) ? 0 : 1;
308}
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