VirtualBox

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

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

Additions/x11: sigaction init.

  • 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;
143
144 sigAction.sa_handler = vboxClientSignalHandler;
145 sigemptyset(&sigAction.sa_mask);
146 sigAction.sa_flags = 0;
147 sigaction(SIGHUP, &sigAction, NULL);
148 sigaction(SIGINT, &sigAction, NULL);
149 sigaction(SIGQUIT, &sigAction, NULL);
150 sigaction(SIGABRT, &sigAction, NULL);
151 sigaction(SIGPIPE, &sigAction, NULL);
152 sigaction(SIGALRM, &sigAction, NULL);
153 sigaction(SIGTERM, &sigAction, NULL);
154 sigaction(SIGUSR1, &sigAction, NULL);
155 sigaction(SIGUSR2, &sigAction, NULL);
156}
157
158/**
159 * Print out a usage message and exit with success.
160 */
161void vboxClientUsage(const char *pcszFileName)
162{
163 /* printf is better for i18n than iostream. */
164 printf("Usage: %s [-d|--nodaemon]\n", pcszFileName);
165 printf("Start the VirtualBox X Window System guest services.\n\n");
166 printf("Options:\n");
167 printf(" -d, --nodaemon do not lower privileges and continue running as a system\n");
168 printf(" service\n");
169 exit(0);
170}
171
172/**
173 * The main loop for the VBoxClient daemon.
174 */
175int main(int argc, char *argv[])
176{
177 int rcClipboard, rc = VINF_SUCCESS;
178 const char *pszFileName = RTPathFilename(argv[0]);
179 bool fDaemonise = true;
180
181 if (NULL == pszFileName)
182 pszFileName = "VBoxClient";
183
184 TRACE;
185 /* Parse our option(s) */
186 /** @todo Use RTGetOpt() if the arguments become more complex. */
187 for (int i = 1; i < argc; ++i)
188 {
189 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--nodaemon"))
190 fDaemonise = false;
191 else if (!strcmp(argv[i], "-h") || strcmp(argv[i], "--help"))
192 {
193 vboxClientUsage(pszFileName);
194 exit(0);
195 }
196 else
197 {
198 /* printf is better than iostream for i18n. */
199 printf("%s: unrecognized option `%s'\n", pszFileName, argv[i]);
200 printf("Try `%s --help' for more information\n", pszFileName);
201 exit(1);
202 }
203 }
204 TRACE;
205 if (fDaemonise)
206 {
207 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
208 if (RT_FAILURE(rc))
209 {
210 std::cout << "VBoxClient: failed to daemonize. exiting."<< std::endl;
211 return 1;
212 }
213 }
214 /* Initialise our runtime before all else. */
215 TRACE;
216 RTR3Init(false);
217 if (RT_FAILURE(VbglR3Init()))
218 {
219 std::cout << "Failed to connect to the VirtualBox kernel service" << std::endl;
220 return 1;
221 }
222 TRACE;
223 if (fDaemonise && RT_FAILURE(vboxClientDropPrivileges()))
224 return 1;
225 LogRel(("VBoxClient: starting...\n"));
226 /* Initialise threading in X11 and in Xt. */
227 if (!XInitThreads() || !XtToolkitThreadInitialize())
228 {
229 LogRel(("VBoxClient: error initialising threads in X11, exiting."));
230 return 1;
231 }
232 /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
233 XSetErrorHandler(vboxClientXLibErrorHandler);
234 /* Set an X11 I/O error handler, so that we can shutdown properly on fatal errors. */
235 gpfnOldIOErrorHandler = XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
236 vboxClientSetSignalHandlers();
237 try
238 {
239#ifdef VBOX_X11_CLIPBOARD
240 /* Connect to the host clipboard. */
241 LogRel(("VBoxClient: starting clipboard Guest Additions...\n"));
242 rcClipboard = gClipboard.init();
243 if (RT_FAILURE(rcClipboard))
244 {
245 LogRel(("VBoxClient: vboxClipboardConnect failed with rc = %Rrc\n", rc));
246 }
247#endif /* VBOX_X11_CLIPBOARD defined */
248#ifdef DYNAMIC_RESIZE
249 LogRel(("VBoxClient: starting dynamic guest resizing...\n"));
250 rc = gDisplayChange.init();
251 if (RT_FAILURE(rc))
252 {
253 LogRel(("VBoxClient: failed to start dynamic guest resizing, rc = %Rrc\n", rc));
254 }
255# ifdef SEAMLESS_GUEST
256 if (RT_SUCCESS(rc))
257 {
258 LogRel(("VBoxClient: starting seamless Guest Additions...\n"));
259 rc = gSeamless.init();
260 if (RT_FAILURE(rc))
261 {
262 LogRel(("VBoxClient: failed to start seamless Additions, rc = %Rrc\n", rc));
263 }
264 }
265# endif /* SEAMLESS_GUEST defined */
266#endif /* DYNAMIC_RESIZE defined */
267 }
268 catch (std::exception e)
269 {
270 LogRel(("VBoxClient: failed to initialise Guest Additions - caught exception: %s\n", e.what()));
271 rc = VERR_UNRESOLVED_ERROR;
272 }
273 catch (...)
274 {
275 LogRel(("VBoxClient: failed to initialise Guest Additions - caught unknown exception.\n"));
276 rc = VERR_UNRESOLVED_ERROR;
277 }
278 LogRel(("VBoxClient: sleeping...\n"));
279 pause();
280 LogRel(("VBoxClient: exiting...\n"));
281 try
282 {
283#ifdef DYNAMIC_RESIZE
284# ifdef SEAMLESS_GUEST
285 LogRel(("VBoxClient: shutting down seamless Guest Additions...\n"));
286 gSeamless.uninit(2000);
287# endif /* SEAMLESS_GUEST defined */
288 LogRel(("VBoxClient: shutting down dynamic guest resizing...\n"));
289 gDisplayChange.uninit(2000);
290#endif /* DYNAMIC_RESIZE defined */
291#ifdef VBOX_X11_CLIPBOARD
292 /* Connect to the host clipboard. */
293 LogRel(("VBoxClient: shutting down clipboard Guest Additions...\n"));
294 gClipboard.uninit(2000);
295#endif /* VBOX_X11_CLIPBOARD defined */
296 }
297 catch (std::exception e)
298 {
299 LogRel(("VBoxClient: failed to shut down Guest Additions - caught exception: %s\n", e.what()));
300 rc = VERR_UNRESOLVED_ERROR;
301 }
302 catch (...)
303 {
304 LogRel(("VBoxClient: failed to shut down Guest Additions - caught unknown exception.\n"));
305 rc = VERR_UNRESOLVED_ERROR;
306 }
307 VbglR3Term();
308 return RT_SUCCESS(rc) ? 0 : 1;
309}
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