VirtualBox

Ignore:
Timestamp:
Feb 4, 2021 12:09:16 AM (4 years ago)
Author:
vboxsync
Message:

NAT/Net: Ignore VBoxNetBaseService::init() and do COM init here with
better error reporting. The base class version is a bit naive and
fixing that would just create unnecessary churn for little immediate
gain, will do the refactoring later. While here make the check of
parseOpt() return code less confusing and LogRel() a "Terminating"
message before exiting to make it more clear in the release log that
the process has terminated orderly. bugref:9929.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/NetworkServices/NAT/VBoxNetLwipNAT.cpp

    r87596 r87598  
    172172private:
    173173    int logInit();
     174    int comInit();
    174175
    175176    static void reportError(const char *a_pcszFormat, ...) RT_IPRT_FORMAT_ATTR(1, 2);
     
    322323    LogFlowFuncEnter();
    323324
    324     /* virtualbox initialized in the superclass */
    325     rc = VBoxNetBaseService::init();
     325    /* Get the COM API set up. */
     326    rc = comInit();
    326327    if (RT_FAILURE(rc))
    327328        return rc;
    328329
    329330    /*
    330      * We get the network name on the command line.  Get hold of our
    331      * API object.
     331     * We get the network name on the command line.  Get hold of its
     332     * API object to get the rest of the configuration from.
    332333     */
    333334    const std::string &networkName = getNetworkName();
     
    341342
    342343    /*
    343      * Now that we know the network name we can create the release log file.
     344     * Now that we know the network name and have ensured that it
     345     * indeed exists we can create the release log file.
    344346     */
    345347    logInit();
     
    499501    LogFlowFuncLeaveRC(rc);
    500502    return rc;
     503}
     504
     505
     506/**
     507 * Primary COM initialization performed on the main thread.
     508 *
     509 * This initializes COM and obtains VirtualBox Client and VirtualBox
     510 * objects.
     511 *
     512 * @note The member variables for them are in the base class.  We
     513 * currently do it here so that we can report errors properly, because
     514 * the base class' VBoxNetBaseService::init() is a bit naive and
     515 * fixing that would just create unnecessary churn for little
     516 * immediate gain.  It's easier to ignore the base class code and do
     517 * it ourselves and do the refactoring later.
     518 */
     519int VBoxNetLwipNAT::comInit()
     520{
     521    HRESULT hrc;
     522
     523    hrc = com::Initialize();
     524    if (FAILED(hrc))
     525    {
     526#ifdef VBOX_WITH_XPCOM
     527        if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
     528        {
     529            char szHome[RTPATH_MAX] = "";
     530            int vrc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
     531            if (RT_SUCCESS(vrc))
     532            {
     533                return RTMsgErrorExit(RTEXITCODE_INIT,
     534                                      "Failed to initialize COM: %s: %Rhrf",
     535                                      szHome, hrc);
     536            }
     537        }
     538#endif  /* VBOX_WITH_XPCOM */
     539        return RTMsgErrorExit(RTEXITCODE_INIT,
     540                              "Failed to initialize COM: %Rhrf", hrc);
     541    }
     542
     543    hrc = virtualboxClient.createInprocObject(CLSID_VirtualBoxClient);
     544    if (FAILED(hrc))
     545    {
     546        reportError("Failed to create VirtualBox Client object: %Rhra", hrc);
     547        return VERR_GENERAL_FAILURE;
     548    }
     549
     550    hrc = virtualboxClient->COMGETTER(VirtualBox)(virtualbox.asOutParam());
     551    if (FAILED(hrc))
     552    {
     553        reportError("Failed to obtain VirtualBox object: %Rhra", hrc);
     554        return VERR_GENERAL_FAILURE;
     555    }
     556
     557    return VINF_SUCCESS;
    501558}
    502559
     
    15631620extern "C" DECLEXPORT(int) TrustedMain(int argc, char **argv, char **envp)
    15641621{
    1565     HRESULT hrc;
    15661622    int rc;
    15671623
     
    15821638#endif
    15831639
    1584     hrc = com::Initialize();
    1585     if (FAILED(hrc))
    1586     {
    1587 #ifdef VBOX_WITH_XPCOM
    1588         if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
    1589         {
    1590             char szHome[RTPATH_MAX] = "";
    1591             int vrc = com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome), false);
    1592             if (RT_SUCCESS(vrc))
    1593             {
    1594                 return RTMsgErrorExit(RTEXITCODE_INIT,
    1595                                       "Failed to initialize COM: %s: %Rhrf",
    1596                                       szHome, hrc);
    1597             }
    1598         }
    1599 #endif  // VBOX_WITH_XPCOM
    1600         return RTMsgErrorExit(RTEXITCODE_INIT,
    1601                               "Failed to initialize COM: %Rhrf", hrc);
    1602     }
    1603 
    16041640    VBoxNetLwipNAT NAT;
    16051641
    1606     Log2(("NAT: initialization\n"));
    1607     rc = NAT.parseArgs(argc - 1, argv + 1);
    1608     rc = (rc == 0) ? VINF_SUCCESS : VERR_GENERAL_FAILURE; /* XXX: FIXME */
    1609 
    1610     if (RT_SUCCESS(rc))
    1611         rc = NAT.init();
    1612 
    1613     if (RT_SUCCESS(rc))
    1614         NAT.run();
    1615 
     1642    int rcExit = NAT.parseArgs(argc - 1, argv + 1);
     1643    if (rcExit != RTEXITCODE_SUCCESS)
     1644        return rcExit;          /* messages are already printed */
     1645
     1646    rc = NAT.init();
     1647    if (RT_FAILURE(rc))
     1648        return RTEXITCODE_INIT;
     1649
     1650    NAT.run();
     1651
     1652    LogRel(("Terminating\n"));
    16161653    return RTEXITCODE_SUCCESS;
    16171654}
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