VirtualBox

Changeset 52000 in vbox for trunk/src/VBox/HostDrivers


Ignore:
Timestamp:
Jul 11, 2014 10:17:14 PM (10 years ago)
Author:
vboxsync
Message:

SUPInstall should start the service too now. Cleaned up exit code handling and cured the annoying assertion if the driver is already installed.

Location:
trunk/src/VBox/HostDrivers/Support
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/HostDrivers/Support/testcase/SUPInstall.cpp

    r39091 r52000  
    4545    if (RT_SUCCESS(rc))
    4646    {
    47         RTMsgInfo("installed successfully");
     47        if (rc == VINF_SUCCESS)
     48            RTMsgInfo("Installed successfully!");
     49        else if (rc == VINF_ALREADY_INITIALIZED)
     50            RTMsgInfo("Already loaded.");
     51        else if (rc == VWRN_ALREADY_EXISTS)
     52            RTMsgInfo("Service already existed; started successfully.");
     53        else
     54            RTMsgInfo("Unexpected status: %Rrc", rc);
    4855        return RTEXITCODE_SUCCESS;
    4956    }
  • trunk/src/VBox/HostDrivers/Support/win/SUPLib-win.cpp

    r51978 r52000  
    229229int suplibOsInstall(void)
    230230{
    231     return suplibOsCreateService();
     231    int rc = suplibOsCreateService();
     232    if (RT_SUCCESS(rc))
     233    {
     234        int rc2 = suplibOsStartService();
     235        if (rc2 != VINF_SUCCESS)
     236            rc = rc2;
     237    }
     238    return rc;
    232239}
    233240
     
    236243{
    237244    int rc = suplibOsStopService();
    238     if (!rc)
     245    if (RT_SUCCESS(rc))
    239246        rc = suplibOsDeleteService();
    240247    return rc;
     
    245252 * Creates the service.
    246253 *
    247  * @returns 0 on success.
    248  * @returns -1 on failure.
     254 * @returns VBox status code.
     255 * @retval  VWRN_ALREADY_EXISTS if it already exists.
    249256 */
    250257static int suplibOsCreateService(void)
     
    253260     * Assume it didn't exist, so we'll create the service.
    254261     */
    255     SC_HANDLE   hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
    256     DWORD LastError = GetLastError(); NOREF(LastError);
    257     AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed rc=%d\n", LastError));
    258     if (hSMgrCreate)
     262    int        rc;
     263    SC_HANDLE hSMgrCreate = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
     264    DWORD     dwErr = GetLastError();
     265    AssertMsg(hSMgrCreate, ("OpenSCManager(,,create) failed dwErr=%d\n", dwErr));
     266    if (hSMgrCreate != NULL)
    259267    {
    260268        char szDriver[RTPATH_MAX];
    261         int rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxDrv.sys"));
     269        rc = RTPathExecDir(szDriver, sizeof(szDriver) - sizeof("\\VBoxDrv.sys"));
    262270        if (RT_SUCCESS(rc))
    263271        {
     
    272280                                               szDriver,
    273281                                               NULL, NULL, NULL, NULL, NULL);
    274             DWORD LastError = GetLastError(); NOREF(LastError);
    275             AssertMsg(hService, ("CreateService failed! LastError=%Rwa szDriver=%s\n", LastError, szDriver));
    276             CloseServiceHandle(hService);
    277             CloseServiceHandle(hSMgrCreate);
    278             return hService ? 0 : -1;
     282            dwErr = GetLastError();
     283            if (hService)
     284            {
     285                CloseServiceHandle(hService);
     286                rc = VINF_SUCCESS;
     287            }
     288            else if (dwErr == ERROR_SERVICE_EXISTS)
     289                rc = VWRN_ALREADY_EXISTS;
     290            else
     291            {
     292                AssertMsgFailed(("CreateService failed! dwErr=%Rwa szDriver=%s\n", dwErr, szDriver));
     293                rc = RTErrConvertFromWin32(dwErr);
     294            }
    279295        }
    280296        CloseServiceHandle(hSMgrCreate);
    281         return rc;
    282     }
    283     return -1;
     297    }
     298    else
     299        rc = RTErrConvertFromWin32(GetLastError());
     300    return rc;
    284301}
    285302
     
    288305 * Stops a possibly running service.
    289306 *
    290  * @returns 0 on success.
    291  * @returns -1 on failure.
     307 * @returns VBox status code.
    292308 */
    293309static int suplibOsStopService(void)
     
    296312     * Assume it didn't exist, so we'll create the service.
    297313     */
    298     int rc = -1;
     314    int         rc;
    299315    SC_HANDLE   hSMgr = OpenSCManager(NULL, NULL, SERVICE_STOP | SERVICE_QUERY_STATUS);
    300     DWORD LastError = GetLastError(); NOREF(LastError);
    301     AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
     316    DWORD       dwErr = GetLastError();
     317    AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed dwErr=%d\n", dwErr));
    302318    if (hSMgr)
    303319    {
     
    311327            QueryServiceStatus(hService, &Status);
    312328            if (Status.dwCurrentState == SERVICE_STOPPED)
    313                 rc = 0;
     329                rc = VINF_SUCCESS;
    314330            else if (ControlService(hService, SERVICE_CONTROL_STOP, &Status))
    315331            {
     
    321337                }
    322338                if (Status.dwCurrentState == SERVICE_STOPPED)
    323                     rc = 0;
     339                    rc = VINF_SUCCESS;
    324340                else
    325                    AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
     341                {
     342                    AssertMsgFailed(("Failed to stop service. status=%d\n", Status.dwCurrentState));
     343                    rc = VERR_GENERAL_FAILURE;
     344                }
    326345            }
    327346            else
    328347            {
    329                 DWORD LastError = GetLastError(); NOREF(LastError);
    330                 AssertMsgFailed(("ControlService failed with LastError=%Rwa. status=%d\n", LastError, Status.dwCurrentState));
     348                dwErr = GetLastError();
     349                AssertMsgFailed(("ControlService failed with dwErr=%Rwa. status=%d\n", dwErr, Status.dwCurrentState));
     350                rc = RTErrConvertFromWin32(dwErr);
    331351            }
    332352            CloseServiceHandle(hService);
    333353        }
    334         else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
    335             rc = 0;
    336354        else
    337355        {
    338             DWORD LastError = GetLastError(); NOREF(LastError);
    339             AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
     356            dwErr = GetLastError();
     357            if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
     358                rc = VINF_SUCCESS;
     359            else
     360            {
     361                AssertMsgFailed(("OpenService failed dwErr=%Rwa\n", dwErr));
     362                rc = RTErrConvertFromWin32(dwErr);
     363            }
    340364        }
    341365        CloseServiceHandle(hSMgr);
     
    348372 * Deletes the service.
    349373 *
    350  * @returns 0 on success.
    351  * @returns -1 on failure.
     374 * @returns VBox status code.
    352375 */
    353376int suplibOsDeleteService(void)
     
    356379     * Assume it didn't exist, so we'll create the service.
    357380     */
    358     int rc = -1;
     381    int         rc;
    359382    SC_HANDLE   hSMgr = OpenSCManager(NULL, NULL, SERVICE_CHANGE_CONFIG);
    360     DWORD LastError = GetLastError(); NOREF(LastError);
    361     AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", LastError));
     383    DWORD       dwErr = GetLastError();
     384    AssertMsg(hSMgr, ("OpenSCManager(,,delete) failed rc=%d\n", dwErr));
    362385    if (hSMgr)
    363386    {
     
    369392             */
    370393            if (DeleteService(hService))
    371                 rc = 0;
     394                rc = VINF_SUCCESS;
    372395            else
    373396            {
    374                 DWORD LastError = GetLastError(); NOREF(LastError);
    375                 AssertMsgFailed(("DeleteService failed LastError=%Rwa\n", LastError));
     397                dwErr = GetLastError();
     398                AssertMsgFailed(("DeleteService failed dwErr=%Rwa\n", dwErr));
     399                rc = RTErrConvertFromWin32(dwErr);
    376400            }
    377401            CloseServiceHandle(hService);
    378402        }
    379         else if (GetLastError() == ERROR_SERVICE_DOES_NOT_EXIST)
    380             rc = 0;
    381403        else
    382404        {
    383             DWORD LastError = GetLastError(); NOREF(LastError);
    384             AssertMsgFailed(("OpenService failed LastError=%Rwa\n", LastError));
     405            dwErr = GetLastError();
     406            if (dwErr == ERROR_SERVICE_DOES_NOT_EXIST)
     407                rc = VINF_SUCCESS;
     408            else
     409            {
     410                AssertMsgFailed(("OpenService failed dwErr=%Rwa\n", dwErr));
     411                rc = RTErrConvertFromWin32(dwErr);
     412            }
    385413        }
    386414        CloseServiceHandle(hSMgr);
     
    453481 * Attempts to start the service, creating it if necessary.
    454482 *
    455  * @returns 0 on success.
    456  * @returns -1 on failure.
    457  * @param   fRetry  Indicates retry call.
     483 * @returns VBox status code.
    458484 */
    459485static int suplibOsStartService(void)
     
    465491    if (hSMgr == NULL)
    466492    {
    467         AssertMsgFailed(("couldn't open service manager in SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS mode!\n"));
    468         return -1;
     493        DWORD dwErr = GetLastError();
     494        AssertMsgFailed(("couldn't open service manager in SERVICE_QUERY_CONFIG | SERVICE_QUERY_STATUS mode! (dwErr=%d)\n", dwErr));
     495        return RTErrConvertFromWin32(dwErr);
    469496    }
    470497
     
    479506         */
    480507        int rc = suplibOsCreateService();
    481         if (rc)
     508        if (RT_FAILURE(rc))
    482509            return rc;
    483510
     
    491518     * Check if open and on demand create succeeded.
    492519     */
    493     int rc = -1;
     520    int rc;
    494521    if (hService)
    495522    {
     
    498525         * Query service status to see if we need to start it or not.
    499526         */
    500         SERVICE_STATUS  Status;
     527        SERVICE_STATUS Status;
    501528        BOOL fRc = QueryServiceStatus(hService, &Status);
    502529        Assert(fRc);
    503         if (    Status.dwCurrentState != SERVICE_RUNNING
    504             &&  Status.dwCurrentState != SERVICE_START_PENDING)
    505         {
     530        if (Status.dwCurrentState == SERVICE_RUNNING)
     531            rc = VINF_ALREADY_INITIALIZED;
     532        else
     533        {
     534            if (Status.dwCurrentState == SERVICE_START_PENDING)
     535                rc = VINF_SUCCESS;
     536            else
     537            {
     538                /*
     539                 * Start it.
     540                 */
     541                if (StartService(hService, 0, NULL))
     542                    rc = VINF_SUCCESS;
     543                else
     544                {
     545                    DWORD dwErr = GetLastError();
     546                    AssertMsg(fRc, ("StartService failed with dwErr=%Rwa\n", dwErr));
     547                    rc = RTErrConvertFromWin32(dwErr);
     548                }
     549            }
     550
    506551            /*
    507              * Start it.
     552             * Wait for the service to finish starting.
     553             * We'll wait for 10 seconds then we'll give up.
    508554             */
    509             fRc = StartService(hService, 0, NULL);
    510             DWORD LastError = GetLastError(); NOREF(LastError);
    511 #ifndef DEBUG_bird
    512             AssertMsg(fRc, ("StartService failed with LastError=%Rwa\n", LastError));
    513 #endif
    514         }
    515 
    516         /*
    517          * Wait for the service to finish starting.
    518          * We'll wait for 10 seconds then we'll give up.
    519          */
    520         QueryServiceStatus(hService, &Status);
    521         if (Status.dwCurrentState == SERVICE_START_PENDING)
    522         {
    523             int iWait;
    524             for (iWait = 100; iWait > 0 && Status.dwCurrentState == SERVICE_START_PENDING; iWait--)
    525             {
    526                 Sleep(100);
    527                 QueryServiceStatus(hService, &Status);
    528             }
    529             DWORD LastError = GetLastError(); NOREF(LastError);
    530             AssertMsg(Status.dwCurrentState != SERVICE_RUNNING,
    531                       ("Failed to start. LastError=%Rwa iWait=%d status=%d\n",
    532                        LastError, iWait, Status.dwCurrentState));
    533         }
    534 
    535         if (Status.dwCurrentState == SERVICE_RUNNING)
    536             rc = 0;
     555            QueryServiceStatus(hService, &Status);
     556            if (Status.dwCurrentState == SERVICE_START_PENDING)
     557            {
     558                int iWait;
     559                for (iWait = 100; iWait > 0 && Status.dwCurrentState == SERVICE_START_PENDING; iWait--)
     560                {
     561                    Sleep(100);
     562                    QueryServiceStatus(hService, &Status);
     563                }
     564                DWORD dwErr = GetLastError(); NOREF(dwErr);
     565                AssertMsg(Status.dwCurrentState != SERVICE_RUNNING,
     566                          ("Failed to start. dwErr=%Rwa iWait=%d status=%d\n", dwErr, iWait, Status.dwCurrentState));
     567            }
     568
     569            if (Status.dwCurrentState == SERVICE_RUNNING)
     570                rc = VINF_SUCCESS;
     571            else if (RT_SUCCESS_NP(rc))
     572                rc = VERR_GENERAL_FAILURE;
     573        }
    537574
    538575        /*
     
    543580    else
    544581    {
    545         DWORD LastError = GetLastError(); NOREF(LastError);
    546         AssertMsgFailed(("OpenService failed! LastError=%Rwa\n", LastError));
     582        DWORD dwErr = GetLastError();
     583        AssertMsgFailed(("OpenService failed! LastError=%Rwa\n", dwErr));
     584        rc = RTErrConvertFromWin32(dwErr);
    547585    }
    548586    if (!CloseServiceHandle(hSMgr))
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