VirtualBox

Ignore:
Timestamp:
Feb 14, 2014 10:29:53 AM (11 years ago)
Author:
vboxsync
Message:

Additions/x11/VBoxClient: clean-up in main code.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/x11/VBoxClient/main.cpp

    r50451 r50464  
    332332/**
    333333 * The main loop for the VBoxClient daemon.
     334 * @todo Clean up for readability.
    334335 */
    335336int main(int argc, char *argv[])
    336337{
     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. */
    337348    if (!XInitThreads())
    338349        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);
    361360        return 1;
    362361    }
     
    366365    for (int i = 1; i < argc; ++i)
    367366    {
     367        rc = VERR_INVALID_PARAMETER;
    368368        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);
    369379            fDaemonise = false;
     380        }
    370381        else if (!strcmp(argv[i], "--clipboard"))
    371382        {
    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();
    376386        }
    377387        else if (!strcmp(argv[i], "--display"))
    378388        {
    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();
    383392        }
    384393        else if (!strcmp(argv[i], "--seamless"))
    385394        {
    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();
    390398        }
    391399        else if (!strcmp(argv[i], "--checkhostversion"))
    392400        {
    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();
    397404        }
    398405#ifdef VBOX_WITH_DRAG_AND_DROP
    399406        else if (!strcmp(argv[i], "--draganddrop"))
    400407        {
    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();
    405411        }
    406412#endif /* VBOX_WITH_DRAG_AND_DROP */
    407413        else if (!strcmp(argv[i], "-h") || !strcmp(argv[i], "--help"))
    408414        {
    409             vboxClientUsage(pszFileName);
     415            vboxClientUsage(pcszFileName);
    410416            return 0;
    411417        }
    412418        else
    413419        {
    414             RTPrintf("%s: unrecognized option `%s'\n", pszFileName, argv[i]);
    415             RTPrintf("Try `%s --help' for more information\n", pszFileName);
     420            RTPrintf("%s: unrecognized option `%s'\n", pcszFileName, argv[i]);
     421            RTPrintf("Try `%s --help' for more information\n", pcszFileName);
    416422            return 1;
    417423        }
    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);
    422429        return 1;
    423430    }
    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));
    464471        VbglR3Term();
    465472        return 1;
    466473    }
    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
    481475    rc = startMonitorThread();
    482476    if (RT_FAILURE(rc))
    483     {
    484477        LogRel(("Failed to start the monitor thread (%Rrc).  Exiting.\n",
    485478                 rc));
    486         VBoxClient::CleanUp();
    487     }
    488     g_pService->run(fDaemonise);
     479    else
     480        g_pService->run(fDaemonise);  /* Should never return. */
    489481    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.

© 2024 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette