VirtualBox

source: vbox/trunk/src/VBox/Additions/x11/VBoxClient/main.cpp@ 23794

Last change on this file since 23794 was 21940, checked in by vboxsync, 15 years ago

Additions/X11/VBoxClient: attempt to fix seamless Additions crashes

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
File size: 8.2 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 <sys/types.h>
24#include <stdlib.h> /* For exit */
25#include <stdio.h>
26#include <string.h>
27#include <unistd.h>
28#include <errno.h>
29#include <signal.h>
30
31#include <X11/Xlib.h>
32
33#include <iprt/env.h>
34#include <iprt/initterm.h>
35#include <iprt/path.h>
36#include <iprt/stream.h>
37#include <iprt/string.h>
38#include <VBox/VBoxGuestLib.h>
39#include <VBox/log.h>
40
41#include "VBoxClient.h"
42
43#define TRACE RTPrintf("%s: %d\n", __PRETTY_FUNCTION__, __LINE__); Log(("%s: %d\n", __PRETTY_FUNCTION__, __LINE__))
44
45static int (*gpfnOldIOErrorHandler)(Display *) = NULL;
46
47/** Object representing the service we are running. This has to be global
48 * so that the cleanup routine can access it. */
49VBoxClient::Service *g_pService;
50/** The name of our pidfile. It is global for the benefit of the cleanup
51 * routine. */
52static char *g_pszPidFile;
53/** The file handle of our pidfile. It is global for the benefit of the
54 * cleanup routine. */
55static RTFILE g_hPidFile;
56
57/** Clean up if we get a signal or something. This is extern so that we
58 * can call it from other compilation units. */
59void VBoxClient::CleanUp()
60{
61 if (g_pService)
62 {
63 g_pService->cleanup();
64 delete g_pService;
65 }
66 if (g_pszPidFile && g_hPidFile)
67 VbglR3ClosePidFile(g_pszPidFile, g_hPidFile);
68 VbglR3Term();
69 exit(0);
70}
71
72/**
73 * A standard signal handler which cleans up and exits.
74 */
75void vboxClientSignalHandler(int cSignal)
76{
77 Log(("VBoxClient: terminated with signal %d\n", cSignal));
78 /** Disable seamless mode */
79 RTPrintf(("VBoxClient: terminating...\n"));
80 VBoxClient::CleanUp();
81}
82
83/**
84 * Xlib error handler for certain errors that we can't avoid.
85 */
86int vboxClientXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
87{
88 char errorText[1024];
89
90 XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
91 LogRelFlow(("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));
92 return 0; /* We should never reach this. */
93}
94
95/**
96 * Xlib error handler for fatal errors. This often means that the programme is still running
97 * when X exits.
98 */
99static int vboxClientXLibIOErrorHandler(Display *pDisplay)
100{
101 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"));
102 VBoxClient::CleanUp();
103 return 0; /* We should never reach this. */
104}
105
106/**
107 * Reset all standard termination signals to call our signal handler, which
108 * cleans up and exits.
109 */
110void vboxClientSetSignalHandlers(void)
111{
112 struct sigaction sigAction;
113
114 LogFlowFunc(("\n"));
115 sigAction.sa_handler = vboxClientSignalHandler;
116 sigemptyset(&sigAction.sa_mask);
117 sigAction.sa_flags = 0;
118 sigaction(SIGHUP, &sigAction, NULL);
119 sigaction(SIGINT, &sigAction, NULL);
120 sigaction(SIGQUIT, &sigAction, NULL);
121 sigaction(SIGABRT, &sigAction, NULL);
122 sigaction(SIGPIPE, &sigAction, NULL);
123 sigaction(SIGALRM, &sigAction, NULL);
124 sigaction(SIGTERM, &sigAction, NULL);
125 sigaction(SIGUSR1, &sigAction, NULL);
126 sigaction(SIGUSR2, &sigAction, NULL);
127 LogFlowFunc(("returning\n"));
128}
129
130/**
131 * Print out a usage message and exit with success.
132 */
133void vboxClientUsage(const char *pcszFileName)
134{
135 RTPrintf("Usage: %s --clipboard|--display|--seamless [-d|--nodaemon]\n", pcszFileName);
136 RTPrintf("Start the VirtualBox X Window System guest services.\n\n");
137 RTPrintf("Options:\n");
138 RTPrintf(" --clipboard start the shared clipboard service\n");
139 RTPrintf(" --display start the display management service\n");
140 RTPrintf(" --seamless start the seamless windows service\n");
141 RTPrintf(" -d, --nodaemon continue running as a system service\n");
142 RTPrintf("\n");
143 exit(0);
144}
145
146/**
147 * The main loop for the VBoxClient daemon.
148 */
149int main(int argc, char *argv[])
150{
151 int rcClipboard, rc = VINF_SUCCESS;
152 const char *pszFileName = RTPathFilename(argv[0]);
153 bool fDaemonise = true;
154 /* Have any fatal errors occurred yet? */
155 bool fSuccess = true;
156 /* Do we know which service we wish to run? */
157 bool fHaveService = false;
158
159 if (NULL == pszFileName)
160 pszFileName = "VBoxClient";
161
162 /* Initialise our runtime before all else. */
163 RTR3Init();
164
165 /* Parse our option(s) */
166 /** @todo Use RTGetOpt() if the arguments become more complex. */
167 for (int i = 1; i < argc; ++i)
168 {
169 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--nodaemon"))
170 fDaemonise = false;
171 else if (!strcmp(argv[i], "--clipboard"))
172 {
173 if (g_pService == NULL)
174 g_pService = VBoxClient::GetClipboardService();
175 else
176 fSuccess = false;
177 }
178 else if (!strcmp(argv[i], "--display"))
179 {
180 if (g_pService == NULL)
181 g_pService = VBoxClient::GetDisplayService();
182 else
183 fSuccess = false;
184 }
185 else if (!strcmp(argv[i], "--seamless"))
186 {
187 if (g_pService == NULL)
188 g_pService = VBoxClient::GetSeamlessService();
189 else
190 fSuccess = false;
191 }
192 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
193 {
194 vboxClientUsage(pszFileName);
195 exit(0);
196 }
197 else
198 {
199 RTPrintf("%s: unrecognized option `%s'\n", pszFileName, argv[i]);
200 RTPrintf("Try `%s --help' for more information\n", pszFileName);
201 exit(1);
202 }
203 }
204 if (!fSuccess || !g_pService)
205 {
206 vboxClientUsage(pszFileName);
207 exit(1);
208 }
209 if (fDaemonise)
210 {
211 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
212 if (RT_FAILURE(rc))
213 {
214 RTPrintf("VBoxClient: failed to daemonize. Exiting.\n");
215 Log(("VBoxClient: failed to daemonize. Exiting.\n"));
216#ifdef DEBUG
217 RTPrintf("Error %Rrc\n", rc);
218#endif
219 return 1;
220 }
221 }
222 const char *pszHome = RTEnvGet("HOME");
223 if (pszHome == NULL)
224 {
225 RTPrintf("VBoxClient: failed to get home directory. Exiting.\n");
226 Log(("VBoxClient: failed to get home directory. Exiting.\n"));
227 return 1;
228 }
229 if (RTStrAPrintf(&g_pszPidFile, "%s/%s", pszHome, g_pService->getPidFilePath()) == -1)
230 if (pszHome == NULL)
231 {
232 RTPrintf("VBoxClient: out of memory. Exiting.\n");
233 Log(("VBoxClient: out of memory. Exiting.\n"));
234 return 1;
235 }
236 /* Initialise the guest library. */
237 if (RT_FAILURE(VbglR3InitUser()))
238 {
239 RTPrintf("Failed to connect to the VirtualBox kernel service\n");
240 Log(("Failed to connect to the VirtualBox kernel service\n"));
241 return 1;
242 }
243 if (g_pszPidFile && RT_FAILURE(VbglR3PidFile(g_pszPidFile, &g_hPidFile)))
244 {
245 RTPrintf("Failed to create a pidfile. Exiting.\n");
246 Log(("Failed to create a pidfile. Exiting.\n"));
247 VbglR3Term();
248 return 1;
249 }
250 /* Set signal handlers to clean up on exit. */
251 vboxClientSetSignalHandlers();
252 /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
253 XSetErrorHandler(vboxClientXLibErrorHandler);
254 /* Set an X11 I/O error handler, so that we can shutdown properly on fatal errors. */
255 XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
256 g_pService->run();
257 VBoxClient::CleanUp();
258 return 1; /* We should never get here. */
259}
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