VirtualBox

Changeset 66629 in vbox for trunk/src/VBox/Main/src-client


Ignore:
Timestamp:
Apr 20, 2017 2:05:28 PM (8 years ago)
Author:
vboxsync
Message:

VirtualBoxClientImpl: Style cleaning in the SDS related code; fixed error code logging & handling mess.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-client/VirtualBoxClientImpl.cpp

    r66564 r66629  
    7070{
    7171    ComPtr<IVirtualBoxSDS> aVirtualBoxSDS;
    72 
    73     HRESULT rc = CoCreateInstance(CLSID_VirtualBoxSDS, /* the VirtualBoxSDS object */
    74                                   NULL,                /* no aggregation */
    75                                   CLSCTX_LOCAL_SERVER, /* the object lives in the current process */
    76                                   IID_IVirtualBoxSDS,  /* IID of the interface */
    77                                   (void **)aVirtualBoxSDS.asOutParam());
    78     if (FAILED(rc))
    79     {
    80         Assert(SUCCEEDED(rc));
    81         return rc;
    82     }
    83 
    84     rc = aVirtualBoxSDS->get_VirtualBox(aVirtualBox.asOutParam());
    85     if (FAILED(rc))
    86         Assert(SUCCEEDED(rc));
    87 
    88     return rc;
     72    HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxSDS, /* the VirtualBoxSDS object */
     73                                   NULL,                /* no aggregation */
     74                                   CLSCTX_LOCAL_SERVER, /* the object lives in the current process */
     75                                   IID_IVirtualBoxSDS,  /* IID of the interface */
     76                                   (void **)aVirtualBoxSDS.asOutParam());
     77    AssertComRCReturn(hrc, hrc);
     78
     79    hrc = aVirtualBoxSDS->get_VirtualBox(aVirtualBox.asOutParam());
     80    AssertComRC(hrc);
     81
     82    return hrc;
    8983}
    9084
     
    9286{
    9387    ComPtr<IVirtualBoxSDS> aVirtualBoxSDS;
    94 
    95     HRESULT rc = CoCreateInstance(CLSID_VirtualBoxSDS, /* the VirtualBoxSDS object */
    96                                   NULL,                /* no aggregation */
    97                                   CLSCTX_LOCAL_SERVER, /* the object lives in the current process */
    98                                   IID_IVirtualBoxSDS,  /* IID of the interface */
    99                                   (void **)aVirtualBoxSDS.asOutParam());
    100     if (FAILED(rc))
    101     {
    102         LogRel(("ReleaseVirtualBox - instantiation of IVirtualBoxSDS failed, %x\n", rc));
    103         Assert(SUCCEEDED(rc));
    104         return rc;
    105     }
    106 
    107     rc = aVirtualBoxSDS->ReleaseVirtualBox();
    108     if (FAILED(rc))
    109     {
    110         LogRel(("DeregisterVirtualBox() failed, %x\n", rc));
    111         Assert(SUCCEEDED(rc));
    112     }
    113     return rc;
    114 }
    115 
    116 
    117 DWORD VirtualBoxClient::getServiceAccount(
    118     const wchar_t* wszServiceName,
    119     wchar_t* wszAccountName,
    120     size_t cbAccountNameSize
    121     )
    122 {
    123     SC_HANDLE schSCManager;
    124     SC_HANDLE schService;
    125     LPQUERY_SERVICE_CONFIG pSc = NULL;
    126     DWORD dwBytesNeeded;
    127     int dwError;
    128 
    129     Assert(wszServiceName);
    130     Assert(wszAccountName);
    131     Assert(cbAccountNameSize);
     88    HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxSDS, /* the VirtualBoxSDS object */
     89                                   NULL,                /* no aggregation */
     90                                   CLSCTX_LOCAL_SERVER, /* the object lives in the current process */
     91                                   IID_IVirtualBoxSDS,  /* IID of the interface */
     92                                   (void **)aVirtualBoxSDS.asOutParam());
     93    AssertComRC(hrc);
     94    if (SUCCEEDED(hrc))
     95    {
     96        hrc = aVirtualBoxSDS->ReleaseVirtualBox();
     97        AssertComRC(hrc);
     98        if (FAILED(hrc))
     99            LogRel(("DeregisterVirtualBox() failed, %Rhrc\n", hrc));
     100    }
     101    else
     102        LogRel(("ReleaseVirtualBox - instantiation of IVirtualBoxSDS failed, %Rhrc\n", hrc));
     103    return hrc;
     104}
     105
     106
     107int VirtualBoxClient::getServiceAccount(const wchar_t *pwszServiceName, wchar_t *pwszAccountName, size_t cwcAccountName)
     108{
     109    AssertPtr(pwszServiceName);
     110    AssertPtr(pwszAccountName);
     111    Assert(cwcAccountName);
     112    *pwszAccountName = '\0';
     113
     114    int vrc;
    132115
    133116    // Get a handle to the SCM database.
    134     schSCManager = OpenSCManager( NULL, NULL, SC_MANAGER_ALL_ACCESS);
    135     if (NULL == schSCManager)
    136     {
    137         dwError = GetLastError();
    138         LogRel(("Error: could not open SCM: %Rhrc\n"));
    139         return RTErrConvertFromWin32(dwError);
    140     }
    141 
    142     // Get a handle to the service.
    143     schService = OpenService(schSCManager, wszServiceName, SERVICE_QUERY_CONFIG);
    144     if (schService == NULL)
    145     {
    146         dwError = GetLastError();
    147         CloseServiceHandle(schSCManager);
    148         LogRel(("Error: Could not open service: %Rwc\n", dwError));
    149         return RTErrConvertFromWin32(dwError);
    150     }
    151 
    152     // Get the configuration information.
    153     if (!QueryServiceConfig(schService, NULL, 0, &dwBytesNeeded))
    154     {
    155         dwError = GetLastError();
    156         if (ERROR_INSUFFICIENT_BUFFER == dwError)
     117    SC_HANDLE hSCManager = OpenSCManagerW(NULL /*pwszMachineName*/, NULL /*pwszDatabaseName*/, SC_MANAGER_ALL_ACCESS);
     118    if (hSCManager != NULL)
     119    {
     120        SC_HANDLE hService = OpenServiceW(hSCManager, pwszServiceName, SERVICE_QUERY_CONFIG);
     121        if (hService != NULL)
    157122        {
    158             pSc = (LPQUERY_SERVICE_CONFIG)malloc(dwBytesNeeded);
    159             if (!pSc)
     123            DWORD cbNeeded = sizeof(QUERY_SERVICE_CONFIGW) + 256;
     124            if (!QueryServiceConfigW(hService, NULL, 0, &cbNeeded))
    160125            {
    161                 LogRel(("Error: allocating memory for service config, %Rwc\n", dwError));
    162                 CloseServiceHandle(schSCManager);
    163                 CloseServiceHandle(schService);
    164                 return RTErrConvertFromWin32(dwError);
     126                Assert(GetLastError() == ERROR_INSUFFICIENT_BUFFER);
     127                LPQUERY_SERVICE_CONFIGW pSc = (LPQUERY_SERVICE_CONFIGW)RTMemTmpAllocZ(cbNeeded);
     128                if (pSc)
     129                {
     130                    DWORD cbNeeded2 = 0;
     131                    if (QueryServiceConfigW(hService, pSc, cbNeeded, &cbNeeded2))
     132                    {
     133                        vrc = RTUtf16Copy(pwszAccountName, cwcAccountName, pSc->lpServiceStartName);
     134                        if (RT_FAILURE(vrc))
     135                            LogRel(("Error: SDS service name is too long (%Rrc): %ls\n", vrc, pSc->lpServiceStartName));
     136                    }
     137                    else
     138                    {
     139                        int dwError = GetLastError();
     140                        vrc = RTErrConvertFromWin32(dwError);
     141                        LogRel(("Error: Failed querying service config: %Rwc (%u) -> %Rrc; cbNeeded=%d cbNeeded2=%d\n",
     142                                dwError, dwError, vrc, cbNeeded, cbNeeded2));
     143                    }
     144                    RTMemTmpFree(pSc);
     145                }
     146                else
     147                {
     148                    LogRel(("Error: Failed allocating %#x bytes of memory for service config!\n", cbNeeded));
     149                    vrc = VERR_NO_TMP_MEMORY;
     150                }
    165151            }
    166             if (!QueryServiceConfig(schService, pSc, dwBytesNeeded, &dwBytesNeeded))
     152            else
    167153            {
    168                 dwError = GetLastError();
    169                 LogRel(("Error: error in querying service config, %Rwc\n", dwError));
    170                 free(pSc);
    171                 CloseServiceHandle(schService);
    172                 CloseServiceHandle(schSCManager);
    173                 return RTErrConvertFromWin32(dwError);
     154                AssertLogRelMsgFailed(("Error: QueryServiceConfigW returns success with zero buffer!\n"));
     155                vrc = VERR_IPE_UNEXPECTED_STATUS;
    174156            }
     157            CloseServiceHandle(hService);
    175158        }
    176     }
    177 
    178     dwError = RTUtf16Copy((RTUTF16*)wszAccountName, cbAccountNameSize / sizeof(RTUTF16),
    179         (RTUTF16*)pSc->lpServiceStartName);
    180     if (RT_FAILURE(dwError))
    181     {
    182         LogRel(("Error: cannot copy account name to destination, %Rrc\n", dwError));
    183         free(pSc);
    184         CloseServiceHandle(schService);
    185         CloseServiceHandle(schSCManager);
    186         return RTErrConvertFromWin32(dwError);
    187     }
    188 
    189     free(pSc);
    190     CloseServiceHandle(schService);
    191     CloseServiceHandle(schSCManager);
    192     return VINF_SUCCESS;
    193 }
    194 
    195 
    196 HRESULT VirtualBoxClient::isServiceDisabled(const wchar_t* wszServiceName, bool* pOutIsDisabled)
     159        else
     160        {
     161            int dwError = GetLastError();
     162            vrc = RTErrConvertFromWin32(dwError);
     163            LogRel(("Error: Could not open service: %Rwc (%u) -> %Rrc\n", dwError, dwError, vrc));
     164        }
     165        CloseServiceHandle(hSCManager);
     166    }
     167    else
     168    {
     169        int dwError = GetLastError();
     170        vrc = RTErrConvertFromWin32(dwError);
     171        LogRel(("Error: Could not open SCM: %Rwc (%u) -> %Rrc\n", dwError, dwError, vrc));
     172    }
     173    return vrc;
     174}
     175
     176
     177HRESULT VirtualBoxClient::isServiceDisabled(const wchar_t *pwszServiceName, bool* pOutIsDisabled)
    197178{
    198179    Assert(pOutIsDisabled);
    199     Assert(wszServiceName);
     180    Assert(pwszServiceName);
    200181    ComPtr<IWbemLocator> aLocator;
    201182    ComPtr<IWbemServices> aService;
     
    229210    hr = aService->ExecQuery(
    230211        com::Bstr("WQL").raw(),
    231         com::BstrFmt("SELECT * FROM Win32_Service WHERE Name='%ls'", wszServiceName).raw(),
     212        com::BstrFmt("SELECT * FROM Win32_Service WHERE Name='%ls'", pwszServiceName).raw(),
    232213        WBEM_FLAG_FORWARD_ONLY | WBEM_FLAG_RETURN_IMMEDIATELY,
    233214        NULL,
     
    412393HRESULT VirtualBoxClient::i_investigateVirtualBoxObjectCreationFailure(HRESULT hrcCaller)
    413394{
    414      /*
     395    /*
    415396     * First step is to try get an IUnknown interface of the VirtualBox object.
    416397     *
     
    421402    IUnknown *pUnknown = NULL;
    422403
    423 #ifdef VBOX_WITH_SDS
     404# ifdef VBOX_WITH_SDS
    424405    // Check the VBOXSDS service running account name is SYSTEM
    425406    wchar_t wszBuffer[256];
    426     int dwError = getServiceAccount(L"VBoxSDS", wszBuffer, sizeof(wszBuffer));
    427     if(RT_FAILURE(dwError))
    428         return setError(hrcCaller, tr("Failed to instantiate CLSID_VirtualBox using VBoxSDS: The VBoxSDS is unavailable: %Rwc"), dwError);
    429 
    430     LogRelFunc(("VBoxSDS service is running under '%ls' account.\n", wszBuffer));
    431 
    432     if(RTUtf16Cmp(L"LocalSystem", wszBuffer) != 0)
     407    int vrc = getServiceAccount(L"VBoxSDS", wszBuffer, RT_ELEMENTS(wszBuffer));
     408    if (RT_FAILURE(vrc))
     409        return setError(hrcCaller,
     410                        tr("Failed to instantiate CLSID_VirtualBox using VBoxSDS: The VBoxSDS is unavailable: %Rrc"), vrc);
     411
     412    LogRelFunc(("VBoxSDS service is running under the '%ls' account.\n", wszBuffer));
     413
     414    if (RTUtf16Cmp(wszBuffer, L"LocalSystem") != 0)
    433415        return setError(hrcCaller,
    434             tr("VBoxSDS should be run under SYSTEM account, but it started under '%ls' account:\n"
    435                 "Change VBoxSDS Windows Service Logon parameters in Service Control Manager. \n%Rhrc"
    436                ), wszBuffer, hrcCaller);
    437 
    438     bool bIsVBoxSDSDisabled = false;
    439     HRESULT hrc = isServiceDisabled(L"VBoxSDS", &bIsVBoxSDSDisabled);
     416                        tr("VBoxSDS should be run under SYSTEM account, but it started under '%ls' account:\n"
     417                           "Change VBoxSDS Windows Service Logon parameters in Service Control Manager. \n%Rhrc"),
     418                       wszBuffer, hrcCaller);
     419
     420    bool fIsVBoxSDSDisabled = false;
     421    HRESULT hrc = isServiceDisabled(L"VBoxSDS", &fIsVBoxSDSDisabled);
    440422    if (FAILED(hrc))
    441423    {
    442424        LogRelFunc(("Warning: Failed to get information about VBoxSDS using WMI:: %Rhrc & %Rhrc", hrcCaller, hrc));
    443         bIsVBoxSDSDisabled = false;
     425        fIsVBoxSDSDisabled = false;
    444426        //return setError(hrcCaller, tr("Failed to get information about VBoxSDS using WMI:: %Rhrc & %Rhrc"), hrcCaller, hrc);
    445427    }
    446     if (bIsVBoxSDSDisabled)
    447     {
    448         return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox using VBoxSDS: "
    449             "VBoxSDS windows service disabled.\n"
    450             "Enable VBoxSDS Windows Service using Windows Service Management Console.\n %Rhrc"), hrcCaller);
     428    if (fIsVBoxSDSDisabled)
     429    {
     430        return setError(hrcCaller,
     431                        tr("Completely failed to instantiate CLSID_VirtualBox using VBoxSDS: "
     432                           "VBoxSDS windows service disabled.\n"
     433                           "Enable VBoxSDS Windows Service using Windows Service Management Console.\n %Rhrc"), hrcCaller);
    451434    }
    452435
     
    468451        return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox using VBoxSDS: %Rhrc & %Rhrc"), hrcCaller, hrc);
    469452    }
    470 #else
     453# else
    471454    HRESULT hrc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_LOCAL_SERVER, IID_IUnknown, (void **)&pUnknown);
    472455    if (FAILED(hrc))
     
    476459        return setError(hrcCaller, tr("Completely failed to instantiate CLSID_VirtualBox: %Rhrc & %Rhrc"), hrcCaller, hrc);
    477460    }
    478 #endif
     461# endif
    479462
    480463    /*
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