1 | /* $Id: main.cpp 76553 2019-01-01 01:45:53Z vboxsync $ */
|
---|
2 | /** @file
|
---|
3 | * VirtualBox Guest Additions - X11 Client.
|
---|
4 | */
|
---|
5 |
|
---|
6 | /*
|
---|
7 | * Copyright (C) 2006-2019 Oracle Corporation
|
---|
8 | *
|
---|
9 | * This file is part of VirtualBox Open Source Edition (OSE), as
|
---|
10 | * available from http://www.virtualbox.org. This file is free software;
|
---|
11 | * you can redistribute it and/or modify it under the terms of the GNU
|
---|
12 | * General Public License (GPL) as published by the Free Software
|
---|
13 | * Foundation, in version 2 as it comes in the "COPYING" file of the
|
---|
14 | * VirtualBox OSE distribution. VirtualBox OSE is distributed in the
|
---|
15 | * hope that it will be useful, but WITHOUT ANY WARRANTY of any kind.
|
---|
16 | */
|
---|
17 |
|
---|
18 |
|
---|
19 | /*********************************************************************************************************************************
|
---|
20 | * Header Files *
|
---|
21 | *********************************************************************************************************************************/
|
---|
22 | #include <sys/types.h>
|
---|
23 | #include <sys/wait.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 <poll.h>
|
---|
30 | #include <signal.h>
|
---|
31 |
|
---|
32 | #include <X11/Xlib.h>
|
---|
33 | #include <X11/Xatom.h>
|
---|
34 |
|
---|
35 | #include <iprt/buildconfig.h>
|
---|
36 | #include <iprt/critsect.h>
|
---|
37 | #include <iprt/env.h>
|
---|
38 | #include <iprt/file.h>
|
---|
39 | #include <iprt/initterm.h>
|
---|
40 | #include <iprt/message.h>
|
---|
41 | #include <iprt/path.h>
|
---|
42 | #include <iprt/param.h>
|
---|
43 | #include <iprt/stream.h>
|
---|
44 | #include <iprt/string.h>
|
---|
45 | #include <iprt/types.h>
|
---|
46 | #include <VBox/VBoxGuestLib.h>
|
---|
47 | #include <VBox/err.h>
|
---|
48 | #include <VBox/log.h>
|
---|
49 |
|
---|
50 | #include "VBoxClient.h"
|
---|
51 |
|
---|
52 |
|
---|
53 | /*********************************************************************************************************************************
|
---|
54 | * Global Variables *
|
---|
55 | *********************************************************************************************************************************/
|
---|
56 | /*static int (*gpfnOldIOErrorHandler)(Display *) = NULL; - unused */
|
---|
57 |
|
---|
58 | /** Object representing the service we are running. This has to be global
|
---|
59 | * so that the cleanup routine can access it. */
|
---|
60 | struct VBCLSERVICE **g_pService;
|
---|
61 | /** The name of our pidfile. It is global for the benefit of the cleanup
|
---|
62 | * routine. */
|
---|
63 | static char g_szPidFile[RTPATH_MAX] = "";
|
---|
64 | /** The file handle of our pidfile. It is global for the benefit of the
|
---|
65 | * cleanup routine. */
|
---|
66 | static RTFILE g_hPidFile;
|
---|
67 | /** Global critical section held during the clean-up routine (to prevent it
|
---|
68 | * being called on multiple threads at once) or things which may not happen
|
---|
69 | * during clean-up (e.g. pausing and resuming the service).
|
---|
70 | */
|
---|
71 | RTCRITSECT g_critSect;
|
---|
72 | /** Counter of how often our deamon has been respawned. */
|
---|
73 | unsigned cRespawn = 0;
|
---|
74 |
|
---|
75 |
|
---|
76 |
|
---|
77 | /**
|
---|
78 | * Exit with a fatal error.
|
---|
79 | *
|
---|
80 | * This is used by the VBClFatalError macro and thus needs to be external.
|
---|
81 | */
|
---|
82 | void vbclFatalError(char *pszMessage)
|
---|
83 | {
|
---|
84 | char *pszCommand;
|
---|
85 | int status;
|
---|
86 | if (pszMessage && cRespawn == 0)
|
---|
87 | {
|
---|
88 | pszCommand = RTStrAPrintf2("notify-send \"VBoxClient: %s\"", pszMessage);
|
---|
89 | if (pszCommand)
|
---|
90 | {
|
---|
91 | status = system(pszCommand);
|
---|
92 | if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
|
---|
93 | {
|
---|
94 | pszCommand = RTStrAPrintf2("xmessage -buttons OK:0 -center \"VBoxClient: %s\"",
|
---|
95 | pszMessage);
|
---|
96 | if (pszCommand)
|
---|
97 | {
|
---|
98 | status = system(pszCommand);
|
---|
99 | if (WEXITSTATUS(status) != 0) /* Utility or extension not available. */
|
---|
100 | {
|
---|
101 | RTPrintf("VBoxClient: %s", pszMessage);
|
---|
102 | }
|
---|
103 | }
|
---|
104 | }
|
---|
105 | }
|
---|
106 | }
|
---|
107 | _exit(RTEXITCODE_FAILURE);
|
---|
108 | }
|
---|
109 |
|
---|
110 | /**
|
---|
111 | * Clean up if we get a signal or something.
|
---|
112 | *
|
---|
113 | * This is extern so that we can call it from other compilation units.
|
---|
114 | */
|
---|
115 | void VBClCleanUp(bool fExit /*=true*/)
|
---|
116 | {
|
---|
117 | /* We never release this, as we end up with a call to exit(3) which is not
|
---|
118 | * async-safe. Unless we fix this application properly, we should be sure
|
---|
119 | * never to exit from anywhere except from this method. */
|
---|
120 | int rc = RTCritSectEnter(&g_critSect);
|
---|
121 | if (RT_FAILURE(rc))
|
---|
122 | VBClFatalError(("VBoxClient: Failure while acquiring the global critical section, rc=%Rrc\n", rc));
|
---|
123 | if (g_pService)
|
---|
124 | (*g_pService)->cleanup(g_pService);
|
---|
125 | if (g_szPidFile[0] && g_hPidFile)
|
---|
126 | VbglR3ClosePidFile(g_szPidFile, g_hPidFile);
|
---|
127 | if (fExit)
|
---|
128 | exit(RTEXITCODE_SUCCESS);
|
---|
129 | }
|
---|
130 |
|
---|
131 | /**
|
---|
132 | * A standard signal handler which cleans up and exits.
|
---|
133 | */
|
---|
134 | static void vboxClientSignalHandler(int cSignal)
|
---|
135 | {
|
---|
136 | LogRel(("VBoxClient: terminated with signal %d\n", cSignal));
|
---|
137 | /** Disable seamless mode */
|
---|
138 | RTPrintf(("VBoxClient: terminating...\n"));
|
---|
139 | VBClCleanUp();
|
---|
140 | }
|
---|
141 |
|
---|
142 | /**
|
---|
143 | * Xlib error handler for certain errors that we can't avoid.
|
---|
144 | */
|
---|
145 | static int vboxClientXLibErrorHandler(Display *pDisplay, XErrorEvent *pError)
|
---|
146 | {
|
---|
147 | char errorText[1024];
|
---|
148 |
|
---|
149 | XGetErrorText(pDisplay, pError->error_code, errorText, sizeof(errorText));
|
---|
150 | 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));
|
---|
151 | return 0;
|
---|
152 | }
|
---|
153 |
|
---|
154 | /**
|
---|
155 | * Xlib error handler for fatal errors. This often means that the programme is still running
|
---|
156 | * when X exits.
|
---|
157 | */
|
---|
158 | static int vboxClientXLibIOErrorHandler(Display *pDisplay)
|
---|
159 | {
|
---|
160 | RT_NOREF1(pDisplay);
|
---|
161 | LogRel(("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"));
|
---|
162 | VBClCleanUp();
|
---|
163 | return 0; /* We should never reach this. */
|
---|
164 | }
|
---|
165 |
|
---|
166 | /**
|
---|
167 | * Reset all standard termination signals to call our signal handler, which
|
---|
168 | * cleans up and exits.
|
---|
169 | */
|
---|
170 | static void vboxClientSetSignalHandlers(void)
|
---|
171 | {
|
---|
172 | struct sigaction sigAction;
|
---|
173 |
|
---|
174 | LogRelFlowFunc(("\n"));
|
---|
175 | sigAction.sa_handler = vboxClientSignalHandler;
|
---|
176 | sigemptyset(&sigAction.sa_mask);
|
---|
177 | sigAction.sa_flags = 0;
|
---|
178 | sigaction(SIGHUP, &sigAction, NULL);
|
---|
179 | sigaction(SIGINT, &sigAction, NULL);
|
---|
180 | sigaction(SIGQUIT, &sigAction, NULL);
|
---|
181 | sigaction(SIGPIPE, &sigAction, NULL);
|
---|
182 | sigaction(SIGALRM, &sigAction, NULL);
|
---|
183 | sigaction(SIGTERM, &sigAction, NULL);
|
---|
184 | sigaction(SIGUSR1, &sigAction, NULL);
|
---|
185 | sigaction(SIGUSR2, &sigAction, NULL);
|
---|
186 | LogRelFlowFunc(("returning\n"));
|
---|
187 | }
|
---|
188 |
|
---|
189 | /**
|
---|
190 | * Print out a usage message and exit with success.
|
---|
191 | */
|
---|
192 | static void vboxClientUsage(const char *pcszFileName)
|
---|
193 | {
|
---|
194 | RTPrintf("Usage: %s --clipboard|"
|
---|
195 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
196 | "--draganddrop|"
|
---|
197 | #endif
|
---|
198 | "--display|"
|
---|
199 | # ifdef VBOX_WITH_GUEST_PROPS
|
---|
200 | "--checkhostversion|"
|
---|
201 | #endif
|
---|
202 | "--seamless|check3d|"
|
---|
203 | "--vmsvga|--vmsvga-x11"
|
---|
204 | "[-d|--nodaemon]\n", pcszFileName);
|
---|
205 | RTPrintf("Starts the VirtualBox DRM/X Window System guest services.\n\n");
|
---|
206 | RTPrintf("Options:\n");
|
---|
207 | RTPrintf(" --clipboard starts the shared clipboard service\n");
|
---|
208 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
209 | RTPrintf(" --draganddrop starts the drag and drop service\n");
|
---|
210 | #endif
|
---|
211 | RTPrintf(" --display starts the display management service\n");
|
---|
212 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
213 | RTPrintf(" --checkhostversion starts the host version notifier service\n");
|
---|
214 | #endif
|
---|
215 | RTPrintf(" --check3d tests whether 3D pass-through is enabled\n");
|
---|
216 | RTPrintf(" --seamless starts the seamless windows service\n");
|
---|
217 | RTPrintf(" --vmsvga starts VMSVGA dynamic resizing for DRM\n");
|
---|
218 | RTPrintf(" --vmsvga-x11 starts VMSVGA dynamic resizing for X11\n");
|
---|
219 | RTPrintf(" -f, --foreground run in the foreground (no daemonizing)\n");
|
---|
220 | RTPrintf(" -d, --nodaemon continues running as a system service\n");
|
---|
221 | RTPrintf(" -h, --help shows this help text\n");
|
---|
222 | RTPrintf(" -V, --version shows version information\n");
|
---|
223 | RTPrintf("\n");
|
---|
224 | }
|
---|
225 |
|
---|
226 | /**
|
---|
227 | * Complains about seeing more than one service specification.
|
---|
228 | *
|
---|
229 | * @returns RTEXITCODE_SYNTAX.
|
---|
230 | */
|
---|
231 | static int vbclSyntaxOnlyOneService(void)
|
---|
232 | {
|
---|
233 | RTMsgError("More than one service specified! Only one, please.");
|
---|
234 | return RTEXITCODE_SYNTAX;
|
---|
235 | }
|
---|
236 |
|
---|
237 | /**
|
---|
238 | * The main loop for the VBoxClient daemon.
|
---|
239 | * @todo Clean up for readability.
|
---|
240 | */
|
---|
241 | int main(int argc, char *argv[])
|
---|
242 | {
|
---|
243 | /* Initialise our runtime before all else. */
|
---|
244 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
245 | if (RT_FAILURE(rc))
|
---|
246 | return RTMsgInitFailure(rc);
|
---|
247 |
|
---|
248 | /* This should never be called twice in one process - in fact one Display
|
---|
249 | * object should probably never be used from multiple threads anyway. */
|
---|
250 | if (!XInitThreads())
|
---|
251 | VBClFatalError(("Failed to initialize X11 threads\n"));
|
---|
252 |
|
---|
253 | /* Get our file name for usage info and hints. */
|
---|
254 | const char *pcszFileName = RTPathFilename(argv[0]);
|
---|
255 | if (!pcszFileName)
|
---|
256 | pcszFileName = "VBoxClient";
|
---|
257 |
|
---|
258 | /* Parse our option(s) */
|
---|
259 | /** @todo Use RTGetOpt() if the arguments become more complex. */
|
---|
260 | bool fDaemonise = true;
|
---|
261 | bool fRespawn = true;
|
---|
262 | for (int i = 1; i < argc; ++i)
|
---|
263 | {
|
---|
264 | if ( !strcmp(argv[i], "-f")
|
---|
265 | || !strcmp(argv[i], "--foreground")
|
---|
266 | || !strcmp(argv[i], "-d")
|
---|
267 | || !strcmp(argv[i], "--nodaemon"))
|
---|
268 | {
|
---|
269 | /* If the user is running in "no daemon" mode anyway, send critical
|
---|
270 | * logging to stdout as well. */
|
---|
271 | /** @todo r=bird: Since the release logger isn't created until the service
|
---|
272 | * calls VbglR3InitUser or VbglR3Init or RTLogCreate, this whole
|
---|
273 | * exercise is pointless. Added --init-vbgl-user and --init-vbgl-full
|
---|
274 | * for getting some work done. */
|
---|
275 | PRTLOGGER pReleaseLog = RTLogRelGetDefaultInstance();
|
---|
276 | if (pReleaseLog)
|
---|
277 | rc = RTLogDestinations(pReleaseLog, "stdout");
|
---|
278 | if (pReleaseLog && RT_FAILURE(rc))
|
---|
279 | return RTMsgErrorExitFailure("failed to redivert error output, rc=%Rrc", rc);
|
---|
280 |
|
---|
281 | fDaemonise = false;
|
---|
282 | if ( !strcmp(argv[i], "-f")
|
---|
283 | || !strcmp(argv[i], "--foreground"))
|
---|
284 | fRespawn = false;
|
---|
285 | }
|
---|
286 | else if (!strcmp(argv[i], "--no-respawn"))
|
---|
287 | {
|
---|
288 | fRespawn = false;
|
---|
289 | }
|
---|
290 | else if (!strcmp(argv[i], "--clipboard"))
|
---|
291 | {
|
---|
292 | if (g_pService)
|
---|
293 | return vbclSyntaxOnlyOneService();
|
---|
294 | g_pService = VBClGetClipboardService();
|
---|
295 | }
|
---|
296 | else if (!strcmp(argv[i], "--display"))
|
---|
297 | {
|
---|
298 | if (g_pService)
|
---|
299 | return vbclSyntaxOnlyOneService();
|
---|
300 | g_pService = VBClGetDisplayService();
|
---|
301 | }
|
---|
302 | else if (!strcmp(argv[i], "--seamless"))
|
---|
303 | {
|
---|
304 | if (g_pService)
|
---|
305 | return vbclSyntaxOnlyOneService();
|
---|
306 | g_pService = VBClGetSeamlessService();
|
---|
307 | }
|
---|
308 | else if (!strcmp(argv[i], "--checkhostversion"))
|
---|
309 | {
|
---|
310 | if (g_pService)
|
---|
311 | return vbclSyntaxOnlyOneService();
|
---|
312 | g_pService = VBClGetHostVersionService();
|
---|
313 | }
|
---|
314 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
315 | else if (!strcmp(argv[i], "--draganddrop"))
|
---|
316 | {
|
---|
317 | if (g_pService)
|
---|
318 | return vbclSyntaxOnlyOneService();
|
---|
319 | g_pService = VBClGetDragAndDropService();
|
---|
320 | }
|
---|
321 | #endif /* VBOX_WITH_DRAG_AND_DROP */
|
---|
322 | else if (!strcmp(argv[i], "--check3d"))
|
---|
323 | {
|
---|
324 | if (g_pService)
|
---|
325 | return vbclSyntaxOnlyOneService();
|
---|
326 | g_pService = VBClCheck3DService();
|
---|
327 | }
|
---|
328 | else if (!strcmp(argv[i], "--vmsvga"))
|
---|
329 | {
|
---|
330 | if (g_pService)
|
---|
331 | return vbclSyntaxOnlyOneService();
|
---|
332 | g_pService = VBClDisplaySVGAService();
|
---|
333 | }
|
---|
334 | else if (!strcmp(argv[i], "--vmsvga-x11"))
|
---|
335 | {
|
---|
336 | if (g_pService)
|
---|
337 | break;
|
---|
338 | g_pService = VBClDisplaySVGAX11Service();
|
---|
339 | }
|
---|
340 | /* bird: this is just a quick hack to get something out of the LogRel statements in the code. */
|
---|
341 | else if (!strcmp(argv[i], "--init-vbgl-user"))
|
---|
342 | {
|
---|
343 | rc = VbglR3InitUser();
|
---|
344 | if (RT_FAILURE(rc))
|
---|
345 | return RTMsgErrorExitFailure("VbglR3InitUser failed: %Rrc", rc);
|
---|
346 | }
|
---|
347 | else if (!strcmp(argv[i], "--init-vbgl-full"))
|
---|
348 | {
|
---|
349 | rc = VbglR3Init();
|
---|
350 | if (RT_FAILURE(rc))
|
---|
351 | return RTMsgErrorExitFailure("VbglR3Init failed: %Rrc", rc);
|
---|
352 | }
|
---|
353 | else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
|
---|
354 | {
|
---|
355 | vboxClientUsage(pcszFileName);
|
---|
356 | return RTEXITCODE_SUCCESS;
|
---|
357 | }
|
---|
358 | else if (!strcmp(argv[i], "-V") || !strcmp(argv[i], "--version"))
|
---|
359 | {
|
---|
360 | RTPrintf("%sr%s\n", RTBldCfgVersion(), RTBldCfgRevisionStr());
|
---|
361 | return RTEXITCODE_SUCCESS;
|
---|
362 | }
|
---|
363 | else
|
---|
364 | {
|
---|
365 | RTMsgError("unrecognized option `%s'", argv[i]);
|
---|
366 | RTMsgInfo("Try `%s --help' for more information", pcszFileName);
|
---|
367 | return RTEXITCODE_SYNTAX;
|
---|
368 | }
|
---|
369 | }
|
---|
370 | if (!g_pService)
|
---|
371 | {
|
---|
372 | RTMsgError("No service specified. Quitting because nothing to do!");
|
---|
373 | return RTEXITCODE_SYNTAX;
|
---|
374 | }
|
---|
375 |
|
---|
376 | rc = RTCritSectInit(&g_critSect);
|
---|
377 | if (RT_FAILURE(rc))
|
---|
378 | VBClFatalError(("Initialising critical section failed: %Rrc\n", rc));
|
---|
379 | if ((*g_pService)->getPidFilePath)
|
---|
380 | {
|
---|
381 | rc = RTPathUserHome(g_szPidFile, sizeof(g_szPidFile));
|
---|
382 | if (RT_FAILURE(rc))
|
---|
383 | VBClFatalError(("Getting home directory for PID file failed: %Rrc\n", rc));
|
---|
384 | rc = RTPathAppend(g_szPidFile, sizeof(g_szPidFile),
|
---|
385 | (*g_pService)->getPidFilePath());
|
---|
386 | if (RT_FAILURE(rc))
|
---|
387 | VBClFatalError(("Creating PID file path failed: %Rrc\n", rc));
|
---|
388 | if (fDaemonise)
|
---|
389 | rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */, fRespawn, &cRespawn);
|
---|
390 | if (RT_FAILURE(rc))
|
---|
391 | VBClFatalError(("Daemonizing failed: %Rrc\n", rc));
|
---|
392 | if (g_szPidFile[0])
|
---|
393 | rc = VbglR3PidFile(g_szPidFile, &g_hPidFile);
|
---|
394 | if (rc == VERR_FILE_LOCK_VIOLATION) /* Already running. */
|
---|
395 | return RTEXITCODE_SUCCESS;
|
---|
396 | if (RT_FAILURE(rc))
|
---|
397 | VBClFatalError(("Creating PID file failed: %Rrc\n", rc));
|
---|
398 | }
|
---|
399 | /* Set signal handlers to clean up on exit. */
|
---|
400 | vboxClientSetSignalHandlers();
|
---|
401 | #ifndef VBOXCLIENT_WITHOUT_X11
|
---|
402 | /* Set an X11 error handler, so that we don't die when we get unavoidable
|
---|
403 | * errors. */
|
---|
404 | XSetErrorHandler(vboxClientXLibErrorHandler);
|
---|
405 | /* Set an X11 I/O error handler, so that we can shutdown properly on
|
---|
406 | * fatal errors. */
|
---|
407 | XSetIOErrorHandler(vboxClientXLibIOErrorHandler);
|
---|
408 | #endif
|
---|
409 | rc = (*g_pService)->init(g_pService);
|
---|
410 | if (RT_SUCCESS(rc))
|
---|
411 | {
|
---|
412 | rc = (*g_pService)->run(g_pService, fDaemonise);
|
---|
413 | if (RT_FAILURE(rc))
|
---|
414 | LogRel2(("Running service failed: %Rrc\n", rc));
|
---|
415 | }
|
---|
416 | else
|
---|
417 | {
|
---|
418 | /** @todo r=andy Should we return an appropriate exit code if the service failed to init?
|
---|
419 | * Must be tested carefully with our init scripts first. */
|
---|
420 | LogRel2(("Initializing service failed: %Rrc\n", rc));
|
---|
421 | }
|
---|
422 | VBClCleanUp(false /*fExit*/);
|
---|
423 | return RTEXITCODE_SUCCESS;
|
---|
424 | }
|
---|
425 |
|
---|