VirtualBox

Changeset 71716 in vbox for trunk/src/VBox/Main


Ignore:
Timestamp:
Apr 6, 2018 6:16:30 PM (7 years ago)
Author:
vboxsync
Message:

VBoxSDS,VBoxSVC: Replaced the enviornment variable hack (VBOX_SERVICE_PROCESS) for preventing VBoxSDS and VBoxSVC from getting watched as clients, with a special export in the executable (Is_VirtualBox_service_process_like_VBoxSDS_And_VBoxSDS). Some cleanups here and there.

Location:
trunk/src/VBox/Main
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/include/RpcChannelHook.h

    r71160 r71716  
    2626
    2727
    28 typedef void* PRPC_CHANNEL_HOOK;
     28typedef void *PRPC_CHANNEL_HOOK;
    2929
    3030// {CEDA3E95-A46A-4C41-AA01-EFFD856E455C}
     
    3232{ 0xceda3e95, 0xa46a, 0x4c41,{ 0xaa, 0x1, 0xef, 0xfd, 0x85, 0x6e, 0x45, 0x5c } };
    3333
    34 #if defined(__cplusplus)
    35 extern "C"
    36 {
    37 #endif // #if defined(__cplusplus)
    38 
    39     // C wrapper for using in proxy
    40     void SetupClientRpcChannelHook(void);
    41 
    42 #if defined(__cplusplus)
    43 }
    44 #endif // #if defined(__cplusplus)
     34RT_C_DECLS_BEGIN
     35// C wrapper for using in proxy
     36void SetupClientRpcChannelHook(void);
     37RT_C_DECLS_END
    4538
    4639
     
    7871protected:
    7972    const GUID m_ChannelHookID;
    80     static volatile bool s_bChannelRegistered;
    81     static volatile bool s_bVBpoxSDSCalledOnce;
     73    static volatile bool s_fChannelRegistered;
     74    static volatile bool s_fVBpoxSDSCalledOnce;
    8275    static CRpcChannelHook s_RpcChannelHook;
    8376
  • trunk/src/VBox/Main/src-all/win/RpcChannelHook.cpp

    r71160 r71716  
    3131#ifdef RT_OS_WINDOWS
    3232
    33 volatile bool CRpcChannelHook::s_bChannelRegistered = false;
    34 volatile bool CRpcChannelHook::s_bVBpoxSDSCalledOnce = false;
     33volatile bool CRpcChannelHook::s_fChannelRegistered = false;
     34volatile bool CRpcChannelHook::s_fVBpoxSDSCalledOnce = false;
    3535CRpcChannelHook CRpcChannelHook::s_RpcChannelHook;
    3636
     37
    3738/*
    38 *   RpcChannelHook IUnknown interface implementation.
    39 */
    40 
     39 * RpcChannelHook IUnknown interface implementation.
     40 */
    4141
    4242STDMETHODIMP CRpcChannelHook::QueryInterface(REFIID riid, void **ppv)
     
    6969}
    7070
     71
     72/**
     73 * This is a function that we can call from non-C++ proxy stub code.
     74 *
     75 * @note Warning! function is not thread safe!
     76 * @todo Consider using RTONCE to serialize this (though it's likely unncessary
     77 *       due to COM).
     78 */
     79void SetupClientRpcChannelHook(void)
     80{
     81    // register single hook only
     82    if (!CRpcChannelHook::IsChannelHookRegistered())
     83    {
     84        HRESULT hr = CoRegisterChannelHook(RPC_CHANNEL_EXTENSION_GUID, &CRpcChannelHook::s_RpcChannelHook);
     85        NOREF(hr);
     86        Assert(SUCCEEDED(hr));
     87        CRpcChannelHook::RegisterChannelHook();
     88        LogFunc(("Registered RPC client channel hook \n"));
     89    }
     90}
     91
     92
    7193/*
    72 *   C wrapper functions.
    73 */
    74 
    75 // Warning: functions below are not thread safe
    76 extern "C"
    77 {
    78     void SetupClientRpcChannelHook(void)
    79     {
    80         // register single hook only
    81         if (!CRpcChannelHook::IsChannelHookRegistered())
    82         {
    83             HRESULT hr = CoRegisterChannelHook(RPC_CHANNEL_EXTENSION_GUID,
    84                 &CRpcChannelHook::s_RpcChannelHook);
    85             NOREF(hr);
    86             Assert(SUCCEEDED(hr));
    87             CRpcChannelHook::RegisterChannelHook();
    88             LogFunc(("Registered RPC client channel hook \n"));
    89         }
    90     }
    91 }
    92 
    93 
    94 /*
    95 *   Internal functions.
    96 */
     94 * Internal methods.
     95 */
    9796
    9897bool CRpcChannelHook::IsChannelHookRegistered()
    9998{
    100     return ASMAtomicReadBool(&s_bChannelRegistered);
     99    return ASMAtomicReadBool(&s_fChannelRegistered);
    101100}
    102101
     
    104103void CRpcChannelHook::RegisterChannelHook()
    105104{
    106     ASMAtomicWriteBool(&s_bChannelRegistered, true);
     105    ASMAtomicWriteBool(&s_fChannelRegistered, true);
    107106}
    108107
     
    124123}
    125124
    126 /*
    127 * This is callback of RPC channel hook called on COM client when COM method call
    128 * finished on server and response returned.
    129 * We use it to catch a moment when a new VirtualBox object sucessfully instantiated.
    130 * This callback is called in client process - VirtualBox.exe, VBoxManage or custom client
    131 * If it happend we register new API client in VBoxSDS.
    132 * Parameters:
    133 *   uExtent - unique ID of our RPC channel
    134 *   rIID - IID of called server interface (IVirtualBox in our case)
    135 *   pDataBuffer - NULL, such as we have nothing to transfer from server side
    136 *   hrFault - result of called COM server method
    137 */
    138 STDMETHODIMP_(void) CRpcChannelHook::ClientNotify(REFGUID uExtent, REFIID riid,
    139     ULONG cbDataSize, void *pDataBuffer, DWORD lDataRep, HRESULT hrFault)
     125/**
     126 * This is callback of RPC channel hook called on COM client when COM method call
     127 * finished on server and response returned.
     128 * We use it to catch a moment when a new VirtualBox object sucessfully instantiated.
     129 * This callback is called in client process - VirtualBox.exe, VBoxManage or custom client
     130 * If it happend we register new API client in VBoxSDS.
     131 * Parameters:
     132 * @param   uExtent         Unique ID of our RPC channel.
     133 * @param   riid            Interface ID of called server interface (IVirtualBox
     134 *                          in our case).
     135 * @param   cbDataBuffer    ???????????????????
     136 * @param   pDataBuffer     NULL, such as we have nothing to transfer from
     137 *                          server side.
     138 * @param   lDataRep        ???????????
     139 * @param   hrFault         result of called COM server method.
     140 */
     141STDMETHODIMP_(void) CRpcChannelHook::ClientNotify(REFGUID uExtent, REFIID riid, ULONG cbDataSize, void *pDataBuffer,
     142                                                  DWORD lDataRep, HRESULT hrFault)
    140143{
    141144    NOREF(cbDataSize);
     
    143146    NOREF(lDataRep);
    144147
    145     // Check that it created VirtualBox and this is first method called on server
    146     // (CreateInstance)
    147     if (uExtent == m_ChannelHookID &&
    148         riid == IID_IVirtualBox &&
    149         !ASMAtomicReadBool(&s_bVBpoxSDSCalledOnce) &&
    150         SUCCEEDED(hrFault) )
     148    /*
     149     * Check that it created VirtualBox and this is first method called on server
     150     * (CreateInstance)
     151     */
     152    if (   riid == IID_IVirtualBox
     153        && uExtent == m_ChannelHookID
     154        && SUCCEEDED(hrFault)
     155        && !ASMAtomicReadBool(&s_fVBpoxSDSCalledOnce) )
    151156    {
    152157        LogFunc(("Finished call of VirtualBox method\n"));
    153158
    154         ASMAtomicWriteBool(&s_bVBpoxSDSCalledOnce, true);
     159        ASMAtomicWriteBool(&s_fVBpoxSDSCalledOnce, true);
     160
     161        /*
     162         * Connect to VBoxSDS.
     163         * Note: VBoxSDS can handle duplicate calls
     164         */
    155165        ComPtr<IVirtualBoxClientList> ptrClientList;
    156         /*
    157         * Connect to VBoxSDS.
    158         * Note: VBoxSDS can handle duplicate calls
    159         */
    160166        HRESULT hrc = CoCreateInstance(CLSID_VirtualBoxClientList, NULL, CLSCTX_LOCAL_SERVER, IID_IVirtualBoxClientList,
    161             (void **)ptrClientList.asOutParam());
     167                                       (void **)ptrClientList.asOutParam());
    162168        if (SUCCEEDED(hrc))
    163169        {
     
    173179}
    174180
    175 STDMETHODIMP_(void) CRpcChannelHook::ClientFillBuffer(REFGUID uExtent, REFIID riid,
    176     ULONG *pDataSize, void *pDataBuffer)
    177 {
    178     NOREF(uExtent);
    179     NOREF(riid);
    180     NOREF(pDataSize);
    181     NOREF(pDataBuffer);
    182 }
    183 
    184 STDMETHODIMP_(void) CRpcChannelHook::ServerGetSize(REFGUID uExtent, REFIID riid,
    185     HRESULT hrFault, ULONG *pDataSize)
     181STDMETHODIMP_(void) CRpcChannelHook::ClientFillBuffer(REFGUID uExtent, REFIID riid, ULONG *pDataSize, void *pDataBuffer)
     182{
     183    RT_NOREF(uExtent, riid, pDataSize, pDataBuffer);
     184}
     185
     186STDMETHODIMP_(void) CRpcChannelHook::ServerGetSize(REFGUID uExtent, REFIID riid, HRESULT hrFault, ULONG *pDataSize)
    186187{
    187188    // Nothing to send to client side from server side
    188     NOREF(uExtent);
    189     NOREF(riid);
    190     NOREF(hrFault);
     189    RT_NOREF(uExtent, riid, hrFault);
    191190    *pDataSize = 0;
    192191}
    193192
    194 STDMETHODIMP_(void) CRpcChannelHook::ServerNotify(REFGUID uExtent, REFIID riid,
    195     ULONG cbDataSize, void *pDataBuffer, DWORD lDataRep)
     193STDMETHODIMP_(void) CRpcChannelHook::ServerNotify(REFGUID uExtent, REFIID riid, ULONG cbDataSize, void *pDataBuffer,
     194                                                  DWORD lDataRep)
    196195{
    197196    // Nothing to do on server side
    198     NOREF(uExtent);
    199     NOREF(riid);
    200     NOREF(cbDataSize);
    201     NOREF(pDataBuffer);
    202     NOREF(lDataRep);
    203 }
    204 
    205 STDMETHODIMP_(void) CRpcChannelHook::ServerFillBuffer(REFGUID uExtent, REFIID riid,
    206     ULONG *pDataSize, void *pDataBuffer, HRESULT hrFault)
     197    RT_NOREF(uExtent, riid, cbDataSize, pDataBuffer, lDataRep);
     198}
     199
     200STDMETHODIMP_(void) CRpcChannelHook::ServerFillBuffer(REFGUID uExtent, REFIID riid, ULONG *pDataSize, void *pDataBuffer,
     201                                                      HRESULT hrFault)
    207202{
    208203    // Nothing to send to client side from server side
    209     NOREF(uExtent);
    210     NOREF(riid);
    211     NOREF(pDataSize);
    212     NOREF(pDataBuffer);
    213     NOREF(hrFault);
     204    RT_NOREF(uExtent, riid, pDataSize, pDataBuffer, hrFault);
    214205}
    215206
  • trunk/src/VBox/Main/src-all/win/VBoxProxyStub.c

    r71715 r71716  
    148148};
    149149
    150 BOOL IsVBoxServiceProcess(void)
    151 {
    152     if (GetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", NULL, 0) == 0)
    153     {
    154         int res = GetLastError();
    155         if (res != ERROR_ENVVAR_NOT_FOUND)
    156             LogRel(("Error: cannot get service environment variable: %Rrwa\n", res));
    157         return false;
    158     }
    159     return true;
    160 }
    161 
    162150
    163151/**
     
    185173            Log12(("VBoxProxyStub[%u]/DllMain: DLL_PROCESS_ATTACH\n", GetCurrentProcessId()));
    186174
    187             /* Install RPC channel hook to intercept a moment just after VirtualBox object activation.
    188                It's reports to VBoxSDS that a new VirtualBox API client started. */
    189175#ifdef VBOX_SDS_CLIENTS_WATCHER
    190             if(!IsVBoxServiceProcess())
     176            /*
     177             * Install RPC channel hook to intercept a moment just after VirtualBox object activation.
     178             * It's reports to VBoxSDS that a new VirtualBox API client started.
     179             */
     180            if (GetProcAddress(GetModuleHandle(NULL), "Is_VirtualBox_service_process_like_VBoxSDS_And_VBoxSDS") == NULL)
    191181                SetupClientRpcChannelHook();
    192182#endif
  • trunk/src/VBox/Main/src-global/win/VBoxSDS.cpp

    r71160 r71716  
    695695
    696696
    697 int SetServiceEnvFlag()
     697/** Special export that make VBoxProxyStub not register this process as one that
     698 * VBoxSDS should be watching.
     699 */
     700extern "C" DECLEXPORT(void) VBOXCALL Is_VirtualBox_service_process_like_VBoxSDS_And_VBoxSDS(void)
    698701{
    699     int rc = VINF_SUCCESS;
    700     if (!SetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", L""))
    701     {
    702         rc = RTErrConvertFromWin32(GetLastError());
    703         LogRel(("Error: cannot set service environment flag:  %Rrs\n", rc));
    704     }
    705     return rc;
     702    /* never called, just need to be here */
    706703}
    707704
     
    728725     */
    729726    RTR3InitExe(argc, &argv, 0);
    730 
    731     SetServiceEnvFlag();
    732727
    733728    static const RTGETOPTDEF s_aOptions[] =
  • trunk/src/VBox/Main/src-server/win/svcmain.cpp

    r71317 r71716  
    516516void    VirtualBoxClassFactory::i_finishVBoxSvc()
    517517{
    518     LogRelFunc(("Finish work of VBoxSVc and VBoxSDS\n"));
     518    LogRelFunc(("Finish work of VBoxSVC and VBoxSDS\n"));
    519519    if (m_ptrVirtualBoxSDS.isNotNull())
    520520    {
     
    787787
    788788
    789 int SetServiceEnvFlag()
    790 {
    791     int rc = VINF_SUCCESS;
    792     if (!SetEnvironmentVariable(L"VBOX_SERVICE_PROCESS", L""))
    793     {
    794         rc = RTErrConvertFromWin32(GetLastError());
    795         LogRel(("Error: cannot set service environment flag:  %Rrs\n", rc));
    796     }
    797     return rc;
     789/** Special export that make VBoxProxyStub not register this process as one that
     790 * VBoxSDS should be watching.
     791 */
     792extern "C" DECLEXPORT(void) VBOXCALL Is_VirtualBox_service_process_like_VBoxSDS_And_VBoxSDS(void)
     793{
     794    /* never called, just need to be here */
    798795}
    799796
     
    834831     */
    835832    RTR3InitExe(argc, &argv, 0);
    836 
    837     SetServiceEnvFlag();
    838833
    839834    static const RTGETOPTDEF s_aOptions[] =
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