Changeset 50464 in vbox for trunk/src/VBox/Additions/x11/VBoxClient
- Timestamp:
- Feb 14, 2014 10:29:53 AM (11 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Additions/x11/VBoxClient/main.cpp
r50451 r50464 332 332 /** 333 333 * The main loop for the VBoxClient daemon. 334 * @todo Clean up for readability. 334 335 */ 335 336 int main(int argc, char *argv[]) 336 337 { 338 bool fDaemonise = true; 339 int rc; 340 const char *pcszFileName, *pcszStage; 341 342 /* Initialise our runtime before all else. */ 343 rc = RTR3InitExe(argc, &argv, 0); 344 if (RT_FAILURE(rc)) 345 return RTMsgInitFailure(rc); 346 /* This should never be called twice in one process - in fact one Display 347 * object should probably never be used from multiple threads anyway. */ 337 348 if (!XInitThreads()) 338 349 return 1; 339 /* Initialise our runtime before all else. */ 340 int rc = RTR3InitExe(argc, &argv, 0); 341 if (RT_FAILURE(rc)) 342 return RTMsgInitFailure(rc); 343 344 int rcClipboard; 345 const char *pszFileName = RTPathFilename(argv[0]); 346 bool fDaemonise = true; 347 /* Have any fatal errors occurred yet? */ 348 bool fSuccess = true; 349 /* Do we know which service we wish to run? */ 350 bool fHaveService = false; 351 352 if (NULL == pszFileName) 353 pszFileName = "VBoxClient"; 354 355 /* Initialise our global clean-up critical section */ 356 rc = RTCritSectInit(&g_critSect); 357 if (RT_FAILURE(rc)) 358 { 359 /* Of course, this should never happen. */ 360 RTPrintf("%s: Failed to initialise the global critical section, rc=%Rrc\n", pszFileName, rc); 350 /* Get our file name for error output. */ 351 pcszFileName = RTPathFilename(argv[0]); 352 if (!pcszFileName) 353 pcszFileName = "VBoxClient"; 354 /* Initialise the guest library. */ 355 rc = VbglR3InitUser(); 356 if (RT_FAILURE(rc)) 357 { 358 RTPrintf("%s: failed to connect to the VirtualBox kernel service, rc=%Rrc\n", 359 pcszFileName, rc); 361 360 return 1; 362 361 } … … 366 365 for (int i = 1; i < argc; ++i) 367 366 { 367 rc = VERR_INVALID_PARAMETER; 368 368 if (!strcmp(argv[i], "-d") || !strcmp(argv[i], "--nodaemon")) 369 { 370 /* If the user is running in "no daemon" mode anyway, send critical 371 * logging to stdout as well. */ 372 PRTLOGGER pReleaseLog = RTLogRelDefaultInstance(); 373 374 if (pReleaseLog) 375 rc = RTLogDestinations(pReleaseLog, "stdout"); 376 if (pReleaseLog && RT_FAILURE(rc)) 377 RTPrintf("%s: failed to redivert error output, rc=%Rrc\n", 378 pcszFileName, rc); 369 379 fDaemonise = false; 380 } 370 381 else if (!strcmp(argv[i], "--clipboard")) 371 382 { 372 if (g_pService == NULL) 373 g_pService = VBoxClient::GetClipboardService(); 374 else 375 fSuccess = false; 383 if (g_pService) 384 break; 385 g_pService = VBoxClient::GetClipboardService(); 376 386 } 377 387 else if (!strcmp(argv[i], "--display")) 378 388 { 379 if (g_pService == NULL) 380 g_pService = VBoxClient::GetDisplayService(); 381 else 382 fSuccess = false; 389 if (g_pService) 390 break; 391 g_pService = VBoxClient::GetDisplayService(); 383 392 } 384 393 else if (!strcmp(argv[i], "--seamless")) 385 394 { 386 if (g_pService == NULL) 387 g_pService = VBoxClient::GetSeamlessService(); 388 else 389 fSuccess = false; 395 if (g_pService) 396 break; 397 g_pService = VBoxClient::GetSeamlessService(); 390 398 } 391 399 else if (!strcmp(argv[i], "--checkhostversion")) 392 400 { 393 if (g_pService == NULL) 394 g_pService = VBoxClient::GetHostVersionService(); 395 else 396 fSuccess = false; 401 if (g_pService) 402 break; 403 g_pService = VBoxClient::GetHostVersionService(); 397 404 } 398 405 #ifdef VBOX_WITH_DRAG_AND_DROP 399 406 else if (!strcmp(argv[i], "--draganddrop")) 400 407 { 401 if (g_pService == NULL) 402 g_pService = VBoxClient::GetDragAndDropService(); 403 else 404 fSuccess = false; 408 if (g_pService) 409 break; 410 g_pService = VBoxClient::GetDragAndDropService(); 405 411 } 406 412 #endif /* VBOX_WITH_DRAG_AND_DROP */ 407 413 else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help")) 408 414 { 409 vboxClientUsage(p szFileName);415 vboxClientUsage(pcszFileName); 410 416 return 0; 411 417 } 412 418 else 413 419 { 414 RTPrintf("%s: unrecognized option `%s'\n", p szFileName, argv[i]);415 RTPrintf("Try `%s --help' for more information\n", p szFileName);420 RTPrintf("%s: unrecognized option `%s'\n", pcszFileName, argv[i]); 421 RTPrintf("Try `%s --help' for more information\n", pcszFileName); 416 422 return 1; 417 423 } 418 } 419 if (!fSuccess || !g_pService) 420 { 421 vboxClientUsage(pszFileName); 424 rc = VINF_SUCCESS; 425 } 426 if (RT_FAILURE(rc) || !g_pService) 427 { 428 vboxClientUsage(pcszFileName); 422 429 return 1; 423 430 } 424 /* Get the path for the pidfiles */ 425 rc = RTPathUserHome(g_szPidFile, sizeof(g_szPidFile));426 if (RT_FAILURE(rc))427 {428 RTPrintf("VBoxClient: failed to get home directory, rc=%Rrc. Exiting.\n", rc);429 LogRel(("VBoxClient: failed to get home directory, rc=%Rrc. Exiting.\n", rc));430 return 1;431 }432 rc = RTPathAppend(g_szPidFile, sizeof(g_szPidFile), g_pService->getPidFilePath());433 if (RT_FAILURE(rc))434 {435 RTPrintf("VBoxClient: RTPathAppend failed with rc=%Rrc. Exiting.\n", rc);436 LogRel(("VBoxClient: RTPathAppend failed with rc=%Rrc. Exiting.\n", rc));437 return 1;438 }439 440 /* Initialise the guest library. */441 if (RT_FAILURE(VbglR3InitUser()))442 {443 RTPrintf("Failed to connect to the VirtualBox kernel service\n");444 LogRel(("Failed to connect to the VirtualBox kernel service\n"));445 return 1;446 }447 if (fDaemonise)448 {449 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */);450 if (RT_FAILURE(rc))451 {452 RTPrintf("VBoxClient: failed to daemonize. Exiting.\n");453 LogRel(("VBoxClient: failed to daemonize. Exiting.\n"));454 # ifdef DEBUG 455 RTPrintf("Error %Rrc\n", rc);456 # endif 457 return 1;458 }459 } 460 if ( g_szPidFile[0] && RT_FAILURE(VbglR3PidFile(g_szPidFile, &g_hPidFile)))461 { 462 RTPrintf("Failed to create a pidfile. Exiting.\n");463 LogRel(("Failed to create a pidfile. Exiting.\n"));431 432 do { 433 pcszStage = "Initialising critical section"; 434 rc = RTCritSectInit(&g_critSect); 435 if (RT_FAILURE(rc)) 436 break; 437 pcszStage = "Getting home directory for pid-file"; 438 rc = RTPathUserHome(g_szPidFile, sizeof(g_szPidFile)); 439 if (RT_FAILURE(rc)) 440 break; 441 pcszStage = "Creating pid-file path"; 442 rc = RTPathAppend(g_szPidFile, sizeof(g_szPidFile), 443 g_pService->getPidFilePath()); 444 if (RT_FAILURE(rc)) 445 break; 446 pcszStage = "Daemonising"; 447 if (fDaemonise) 448 rc = VbglR3Daemonize(false /* fNoChDir */, false /* fNoClose */); 449 if (RT_FAILURE(rc)) 450 break; 451 pcszStage = "Creating pid-file"; 452 if (g_szPidFile[0]) 453 rc = VbglR3PidFile(g_szPidFile, &g_hPidFile); 454 if (RT_FAILURE(rc)) 455 break; 456 /* Set signal handlers to clean up on exit. */ 457 vboxClientSetSignalHandlers(); 458 /* Set an X11 error handler, so that we don't die when we get unavoidable 459 * errors. */ 460 XSetErrorHandler(vboxClientXLibErrorHandler); 461 /* Set an X11 I/O error handler, so that we can shutdown properly on 462 * fatal errors. */ 463 XSetIOErrorHandler(vboxClientXLibIOErrorHandler); 464 pcszStage = "Initialising service"; 465 rc = g_pService->init(); 466 } while (0); 467 if (RT_FAILURE(rc)) 468 { 469 LogRelFunc(("VBoxClient: failed at stage: \"%s\" rc: %Rrc\n", 470 pcszStage, rc)); 464 471 VbglR3Term(); 465 472 return 1; 466 473 } 467 /* Set signal handlers to clean up on exit. */ 468 vboxClientSetSignalHandlers(); 469 /* Set an X11 error handler, so that we don't die when we get unavoidable errors. */ 470 XSetErrorHandler(vboxClientXLibErrorHandler); 471 /* Set an X11 I/O error handler, so that we can shutdown properly on fatal errors. */ 472 XSetIOErrorHandler(vboxClientXLibIOErrorHandler); 473 rc = g_pService->init(); 474 if (RT_FAILURE(rc)) 475 { 476 LogRel(("VBoxClient: failed to initialise the service (%Rrc). Exiting.\n", 477 rc)); 478 VbglR3Term(); 479 return 1; 480 } 474 481 475 rc = startMonitorThread(); 482 476 if (RT_FAILURE(rc)) 483 {484 477 LogRel(("Failed to start the monitor thread (%Rrc). Exiting.\n", 485 478 rc)); 486 VBoxClient::CleanUp(); 487 } 488 g_pService->run(fDaemonise); 479 else 480 g_pService->run(fDaemonise); /* Should never return. */ 489 481 VBoxClient::CleanUp(); 490 return 1; /* We should never get here. */491 } 482 return 1; 483 }
Note:
See TracChangeset
for help on using the changeset viewer.