1 | /* $Id: main.cpp 80378 2019-08-21 17:58:06Z 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 "
|
---|
195 | #ifdef VBOX_WITH_SHARED_CLIPBOARD
|
---|
196 | "--clipboard|"
|
---|
197 | #endif
|
---|
198 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
199 | "--draganddrop|"
|
---|
200 | #endif
|
---|
201 | "--display|"
|
---|
202 | # ifdef VBOX_WITH_GUEST_PROPS
|
---|
203 | "--checkhostversion|"
|
---|
204 | #endif
|
---|
205 | "--seamless|"
|
---|
206 | "--vmsvga|--vmsvga-x11"
|
---|
207 | "[-d|--nodaemon]\n", pcszFileName);
|
---|
208 | RTPrintf("Starts the VirtualBox DRM/X Window System guest services.\n\n");
|
---|
209 | RTPrintf("Options:\n");
|
---|
210 | #ifdef VBOX_WITH_SHARED_CLIPBOARD
|
---|
211 | RTPrintf(" --clipboard starts the shared clipboard service\n");
|
---|
212 | #endif
|
---|
213 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
214 | RTPrintf(" --draganddrop starts the drag and drop service\n");
|
---|
215 | #endif
|
---|
216 | RTPrintf(" --display starts the display management service\n");
|
---|
217 | #ifdef VBOX_WITH_GUEST_PROPS
|
---|
218 | RTPrintf(" --checkhostversion starts the host version notifier service\n");
|
---|
219 | #endif
|
---|
220 | RTPrintf(" --seamless starts the seamless windows service\n");
|
---|
221 | RTPrintf(" --vmsvga starts VMSVGA dynamic resizing for DRM\n");
|
---|
222 | RTPrintf(" --vmsvga-x11 starts VMSVGA dynamic resizing for X11\n");
|
---|
223 | RTPrintf(" -f, --foreground run in the foreground (no daemonizing)\n");
|
---|
224 | RTPrintf(" -d, --nodaemon continues running as a system service\n");
|
---|
225 | RTPrintf(" -h, --help shows this help text\n");
|
---|
226 | RTPrintf(" -V, --version shows version information\n");
|
---|
227 | RTPrintf("\n");
|
---|
228 | }
|
---|
229 |
|
---|
230 | /**
|
---|
231 | * Complains about seeing more than one service specification.
|
---|
232 | *
|
---|
233 | * @returns RTEXITCODE_SYNTAX.
|
---|
234 | */
|
---|
235 | static int vbclSyntaxOnlyOneService(void)
|
---|
236 | {
|
---|
237 | RTMsgError("More than one service specified! Only one, please.");
|
---|
238 | return RTEXITCODE_SYNTAX;
|
---|
239 | }
|
---|
240 |
|
---|
241 | /**
|
---|
242 | * The main loop for the VBoxClient daemon.
|
---|
243 | * @todo Clean up for readability.
|
---|
244 | */
|
---|
245 | int main(int argc, char *argv[])
|
---|
246 | {
|
---|
247 | /* Initialise our runtime before all else. */
|
---|
248 | int rc = RTR3InitExe(argc, &argv, 0);
|
---|
249 | if (RT_FAILURE(rc))
|
---|
250 | return RTMsgInitFailure(rc);
|
---|
251 |
|
---|
252 | /* This should never be called twice in one process - in fact one Display
|
---|
253 | * object should probably never be used from multiple threads anyway. */
|
---|
254 | if (!XInitThreads())
|
---|
255 | VBClFatalError(("Failed to initialize X11 threads\n"));
|
---|
256 |
|
---|
257 | /* Get our file name for usage info and hints. */
|
---|
258 | const char *pcszFileName = RTPathFilename(argv[0]);
|
---|
259 | if (!pcszFileName)
|
---|
260 | pcszFileName = "VBoxClient";
|
---|
261 |
|
---|
262 | /* Parse our option(s) */
|
---|
263 | /** @todo Use RTGetOpt() if the arguments become more complex. */
|
---|
264 | bool fDaemonise = true;
|
---|
265 | bool fRespawn = true;
|
---|
266 | for (int i = 1; i < argc; ++i)
|
---|
267 | {
|
---|
268 | if ( !strcmp(argv[i], "-f")
|
---|
269 | || !strcmp(argv[i], "--foreground")
|
---|
270 | || !strcmp(argv[i], "-d")
|
---|
271 | || !strcmp(argv[i], "--nodaemon"))
|
---|
272 | {
|
---|
273 | /* If the user is running in "no daemon" mode anyway, send critical
|
---|
274 | * logging to stdout as well. */
|
---|
275 | /** @todo r=bird: Since the release logger isn't created until the service
|
---|
276 | * calls VbglR3InitUser or VbglR3Init or RTLogCreate, this whole
|
---|
277 | * exercise is pointless. Added --init-vbgl-user and --init-vbgl-full
|
---|
278 | * for getting some work done. */
|
---|
279 | PRTLOGGER pReleaseLog = RTLogRelGetDefaultInstance();
|
---|
280 | if (pReleaseLog)
|
---|
281 | rc = RTLogDestinations(pReleaseLog, "stdout");
|
---|
282 | if (pReleaseLog && RT_FAILURE(rc))
|
---|
283 | return RTMsgErrorExitFailure("failed to redivert error output, rc=%Rrc", rc);
|
---|
284 |
|
---|
285 | fDaemonise = false;
|
---|
286 | if ( !strcmp(argv[i], "-f")
|
---|
287 | || !strcmp(argv[i], "--foreground"))
|
---|
288 | fRespawn = false;
|
---|
289 | }
|
---|
290 | else if (!strcmp(argv[i], "--no-respawn"))
|
---|
291 | {
|
---|
292 | fRespawn = false;
|
---|
293 | }
|
---|
294 | #ifdef VBOX_WITH_SHARED_CLIPBOARD
|
---|
295 | else if (!strcmp(argv[i], "--clipboard"))
|
---|
296 | {
|
---|
297 | if (g_pService)
|
---|
298 | return vbclSyntaxOnlyOneService();
|
---|
299 | g_pService = VBClGetClipboardService();
|
---|
300 | }
|
---|
301 | #endif
|
---|
302 | else if (!strcmp(argv[i], "--display"))
|
---|
303 | {
|
---|
304 | if (g_pService)
|
---|
305 | return vbclSyntaxOnlyOneService();
|
---|
306 | g_pService = VBClGetDisplayService();
|
---|
307 | }
|
---|
308 | else if (!strcmp(argv[i], "--seamless"))
|
---|
309 | {
|
---|
310 | if (g_pService)
|
---|
311 | return vbclSyntaxOnlyOneService();
|
---|
312 | g_pService = VBClGetSeamlessService();
|
---|
313 | }
|
---|
314 | else if (!strcmp(argv[i], "--checkhostversion"))
|
---|
315 | {
|
---|
316 | if (g_pService)
|
---|
317 | return vbclSyntaxOnlyOneService();
|
---|
318 | g_pService = VBClGetHostVersionService();
|
---|
319 | }
|
---|
320 | #ifdef VBOX_WITH_DRAG_AND_DROP
|
---|
321 | else if (!strcmp(argv[i], "--draganddrop"))
|
---|
322 | {
|
---|
323 | if (g_pService)
|
---|
324 | return vbclSyntaxOnlyOneService();
|
---|
325 | g_pService = VBClGetDragAndDropService();
|
---|
326 | }
|
---|
327 | #endif /* VBOX_WITH_DRAG_AND_DROP */
|
---|
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 |
|
---|