VirtualBox

Changeset 95274 in vbox for trunk/src/VBox/Main/src-server


Ignore:
Timestamp:
Jun 14, 2022 10:48:33 AM (3 years ago)
Author:
vboxsync
Message:

FE/VBoxAutostart/adi: Added experimental support for running VBoxSVC session 0 (service session). Did some minor cleanup for the patch posted at comment 95. Disabled by default for now. bugref:9341

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Main/src-server/win/svcmain.cpp

    r95120 r95274  
    333333    if (SUCCEEDED(hrc))
    334334    {
    335         /*
    336          * Create VBoxSVCRegistration object and hand that to VBoxSDS.
    337          */
    338         m_pVBoxSVC = new VBoxSVCRegistration(this);
    339         hrc = m_ptrVirtualBoxSDS->RegisterVBoxSVC(m_pVBoxSVC, GetCurrentProcessId(), ppOtherVirtualBox);
     335        /* By default the RPC_C_IMP_LEVEL_IDENTIFY is used for impersonation the client. It allows
     336           ACL checking but restricts an access to system objects e.g. files. Call to CoSetProxyBlanket
     337           elevates the impersonation level up to RPC_C_IMP_LEVEL_IMPERSONATE allowing the VBoxSDS
     338           service to access the files. */
     339        hrc = CoSetProxyBlanket(m_ptrVirtualBoxSDS,
     340                                RPC_C_AUTHN_DEFAULT,
     341                                RPC_C_AUTHZ_DEFAULT,
     342                                COLE_DEFAULT_PRINCIPAL,
     343                                RPC_C_AUTHN_LEVEL_DEFAULT,
     344                                RPC_C_IMP_LEVEL_IMPERSONATE,
     345                                NULL,
     346                                EOAC_DEFAULT);
    340347        if (SUCCEEDED(hrc))
    341348        {
    342             g_fRegisteredWithVBoxSDS = !*ppOtherVirtualBox;
    343             return hrc;
    344         }
    345         m_pVBoxSVC->Release();
     349            /*
     350             * Create VBoxSVCRegistration object and hand that to VBoxSDS.
     351             */
     352            m_pVBoxSVC = new VBoxSVCRegistration(this);
     353            hrc = E_PENDING;
     354            /* we try to register IVirtualBox 10 times */
     355            for (int regTimes = 0; hrc == E_PENDING && regTimes < 10;  --regTimes)
     356            {
     357                hrc = m_ptrVirtualBoxSDS->RegisterVBoxSVC(m_pVBoxSVC, GetCurrentProcessId(), ppOtherVirtualBox);
     358                if (SUCCEEDED(hrc))
     359                {
     360                    g_fRegisteredWithVBoxSDS = !*ppOtherVirtualBox;
     361                    return hrc;
     362                }
     363                /* sleep to give a time for windows session 0 registration */
     364                if (hrc == E_PENDING)
     365                    RTThreadSleep(1000);
     366            }
     367            m_pVBoxSVC->Release();
     368        }
    346369    }
    347370    m_ptrVirtualBoxSDS.setNull();
     
    687710            break;
    688711        }
    689 
    690712        case WM_DESTROY:
    691713        {
     
    823845{
    824846    /* never called, just need to be here */
     847}
     848
     849
     850/* thread for registering the VBoxSVC started in session 0 */
     851static DWORD WINAPI threadRegisterVirtualBox(LPVOID lpParam) throw()
     852{
     853    HANDLE hEvent = (HANDLE)lpParam;
     854    HRESULT hrc = CoInitializeEx(NULL, COINIT_MULTITHREADED);
     855    if (SUCCEEDED(hrc))
     856    {
     857        /* create IVirtualBox instance */
     858        ComPtr<IVirtualBox> pVirtualBox;
     859        hrc = CoCreateInstance(CLSID_VirtualBox, NULL, CLSCTX_INPROC_SERVER /*CLSCTX_LOCAL_SERVER */, IID_IVirtualBox,
     860                               (void **)pVirtualBox.asOutParam());
     861        if (SUCCEEDED(hrc))
     862        {
     863            /* wait a minute allowing clients to connect to the instance */
     864            WaitForSingleObject(hEvent, 60 * 1000);
     865            /* remove reference. If anybody connected to IVirtualBox it will stay alive. */
     866            pVirtualBox.setNull();
     867        }
     868        CoUninitialize();
     869    }
     870    return 0L;
    825871}
    826872
     
    891937        { "-loginterval",   'I',    RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_ICASE },
    892938        { "/loginterval",   'I',    RTGETOPT_REQ_UINT32 | RTGETOPT_FLAG_ICASE },
     939        { "--registervbox", 'b',    RTGETOPT_REQ_NOTHING | RTGETOPT_FLAG_ICASE },
     940        { "-registervbox",  'b',    RTGETOPT_REQ_NOTHING | RTGETOPT_FLAG_ICASE },
     941        { "/registervbox",  'b',    RTGETOPT_REQ_NOTHING | RTGETOPT_FLAG_ICASE },
    893942    };
    894943
     
    901950    uint32_t        uHistoryFileTime = RT_SEC_1DAY; // max 1 day per file
    902951    uint64_t        uHistoryFileSize = 100 * _1M;   // max 100MB per file
     952    bool            fRegisterVBox = false;
    903953
    904954    RTGETOPTSTATE   GetOptState;
     
    9811031            }
    9821032
     1033            case 'b':
     1034                fRegisterVBox = true;
     1035                break;
     1036
    9831037            default:
    9841038                /** @todo this assumes that stderr is visible, which is not
     
    11011155            Log(("SVCMain: Failed to create main window\n"));
    11021156
     1157        /* create thread to register IVirtualBox in VBoxSDS
     1158         * It is used for starting the VBoxSVC in the windows
     1159         * session 0. */
     1160        HANDLE hWaitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
     1161        HANDLE hRegisterVBoxThread = NULL;
     1162        if (fRegisterVBox)
     1163        {
     1164            DWORD  dwThreadId = 0;
     1165            hRegisterVBoxThread = CreateThread(NULL, 0, threadRegisterVirtualBox, (LPVOID)hWaitEvent,
     1166                                               0, &dwThreadId);
     1167        }
     1168
    11031169        MSG msg;
    11041170        while (GetMessage(&msg, 0, 0, 0) > 0)
     
    11091175
    11101176        DestroyMainWindow();
     1177
     1178        if (fRegisterVBox)
     1179        {
     1180            SetEvent(hWaitEvent);
     1181            WaitForSingleObject(hRegisterVBoxThread, INFINITE);
     1182            CloseHandle(hRegisterVBoxThread);
     1183            CloseHandle(hWaitEvent);
     1184        }
    11111185
    11121186        g_pModule->RevokeClassObjects();
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