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 |
|
---|
45 | static 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. */
|
---|
49 | VBoxClient::Service *g_pService;
|
---|
50 | /** The name of our pidfile. It is global for the benefit of the cleanup
|
---|
51 | * routine. */
|
---|
52 | static char *g_pszPidFile;
|
---|
53 | /** The file handle of our pidfile. It is global for the benefit of the
|
---|
54 | * cleanup routine. */
|
---|
55 | static 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. */
|
---|
59 | void 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 | */
|
---|
75 | void 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 | */
|
---|
86 | int 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 | */
|
---|
99 | static 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 | */
|
---|
110 | void 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 | */
|
---|
133 | void 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 | */
|
---|
149 | int 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 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
193 | else if (!strcmp(argv[i], "--checkhostversion"))
|
---|
194 | {
|
---|
195 | if (g_pService == NULL)
|
---|
196 | g_pService = VBoxClient::GetHostVersionService();
|
---|
197 | else
|
---|
198 | fSuccess = false;
|
---|
199 | }
|
---|
200 | #endif
|
---|
201 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
|
---|
202 | {
|
---|
203 | vboxClientUsage(pszFileName);
|
---|
204 | exit(0);
|
---|
205 | }
|
---|
206 | else
|
---|
207 | {
|
---|
208 | RTPrintf("%s: unrecognized option `%s'\n", pszFileName, argv[i]);
|
---|
209 | RTPrintf("Try `%s --help' for more information\n", pszFileName);
|
---|
210 | exit(1);
|
---|
211 | }
|
---|
212 | }
|
---|
213 | if (!fSuccess || !g_pService)
|
---|
214 | {
|
---|
215 | vboxClientUsage(pszFileName);
|
---|
216 | exit(1);
|
---|
217 | }
|
---|
218 | if (fDaemonise)
|
---|
219 | {
|
---|
220 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);
|
---|
221 | if (RT_FAILURE(rc))
|
---|
222 | {
|
---|
223 | RTPrintf("VBoxClient: failed to daemonize. Exiting.\n");
|
---|
224 | Log(("VBoxClient: failed to daemonize. Exiting.\n"));
|
---|
225 | #ifdef DEBUG
|
---|
226 | RTPrintf("Error %Rrc\n", rc);
|
---|
227 | #endif
|
---|
228 | return 1;
|
---|
229 | }
|
---|
230 | }
|
---|
231 | const char *pszHome = RTEnvGet("HOME");
|
---|
232 | if (pszHome == NULL)
|
---|
233 | {
|
---|
234 | RTPrintf("VBoxClient: failed to get home directory. Exiting.\n");
|
---|
235 | Log(("VBoxClient: failed to get home directory. Exiting.\n"));
|
---|
236 | return 1;
|
---|
237 | }
|
---|
238 | if (RTStrAPrintf(&g_pszPidFile, "%s/%s", pszHome, g_pService->getPidFilePath()) == -1)
|
---|
239 | if (pszHome == NULL)
|
---|
240 | {
|
---|
241 | RTPrintf("VBoxClient: out of memory. Exiting.\n");
|
---|
242 | Log(("VBoxClient: out of memory. Exiting.\n"));
|
---|
243 | return 1;
|
---|
244 | }
|
---|
245 | /* Initialise the guest library. */
|
---|
246 | if (RT_FAILURE(VbglR3InitUser()))
|
---|
247 | {
|
---|
248 | RTPrintf("Failed to connect to the VirtualBox kernel service\n");
|
---|
249 | Log(("Failed to connect to the VirtualBox kernel service\n"));
|
---|
250 | return 1;
|
---|
251 | }
|
---|
252 | if (g_pszPidFile && RT_FAILURE(VbglR3PidFile(g_pszPidFile, &g_hPidFile)))
|
---|
253 | {
|
---|
254 | RTPrintf("Failed to create a pidfile. Exiting.\n");
|
---|
255 | Log(("Failed to create a pidfile. Exiting.\n"));
|
---|
256 | VbglR3Term();
|
---|
257 | return 1;
|
---|
258 | }
|
---|
259 | /* Set signal handlers to clean up on exit. */
|
---|
260 | vboxClientSetSignalHandlers();
|
---|
261 | /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */
|
---|
262 | XSetErrorHandler(vboxClientXLibErrorHandler);
|
---|
263 | /* Set an X11 I/O error handler, so that we can shutdown properly on fatal errors. */
|
---|
264 | XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
|
---|
265 | g_pService->run();
|
---|
266 | VBoxClient::CleanUp();
|
---|
267 | return 1; /* We should never get here. */
|
---|
268 | }
|
---|