VirtualBox

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

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

The Big Sun Rebranding Header Change

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