VirtualBox

Ignore:
Timestamp:
Apr 3, 2009 7:44:47 AM (16 years ago)
Author:
vboxsync
svn:sync-xref-src-repo-rev:
45599
Message:

Guest Additions/common: Update.

Location:
trunk/src/VBox/Additions/common/VBoxService
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Additions/common/VBoxService/VBoxService.cpp

    r18648 r18670  
    3636#include <iprt/asm.h>
    3737#include <iprt/path.h>
     38#include <VBox/Log.h>
    3839#include <VBox/VBoxGuest.h>
    3940#include "VBoxServiceInternal.h"
     
    158159int VBoxServiceError(const char *pszFormat, ...)
    159160{
     161    char szBuffer[1024] = { 0 };
    160162    RTStrmPrintf(g_pStdErr, "%s: error: ", g_pszProgName);
    161163
     
    163165    va_start(va, pszFormat);
    164166    RTStrmPrintfV(g_pStdErr, pszFormat, va);
     167
     168    RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va);
     169    LogRel((szBuffer));
     170
    165171    va_end(va);
    166172
     
    184190        va_start(va, pszFormat);
    185191        RTStrmPrintfV(g_pStdOut, pszFormat, va);
     192
     193#ifdef _DEBUG
     194        char szBuffer[1024] = { 0 };
     195        RTStrPrintfV(szBuffer, sizeof(szBuffer), pszFormat, va);
     196        LogRel((szBuffer));
     197#endif
     198
    186199        va_end(va);
    187200    }
     
    239252}
    240253
     254int VBoxServiceStartServices(int iMain)
     255{
     256    int rc = 0;
     257
     258    /*
     259     * Initialize the services.
     260     */
     261    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     262    {
     263        rc = g_aServices[j].pDesc->pfnInit();
     264        if (RT_FAILURE(rc))
     265            return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
     266    }
     267
     268    if (iMain == 0) /* If 0 is specified, start all services. Useful for the Windows SCM stuff. */
     269        iMain = RT_ELEMENTS(g_aServices);
     270
     271    /*
     272     * Start the service(s).
     273     */
     274    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     275    {
     276        if (    !g_aServices[j].fEnabled
     277            ||  j == iMain)
     278            continue;
     279
     280        rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
     281                            RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
     282        if (RT_FAILURE(rc))
     283        {
     284            VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
     285            break;
     286        }
     287        g_aServices[j].fStarted = true;
     288
     289        /* wait for the thread to initialize */
     290        RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
     291        if (g_aServices[j].fShutdown)
     292            rc = VERR_GENERAL_FAILURE;
     293    }
     294    if (RT_SUCCESS(rc))
     295    {
     296        /* The final service runs in the main thread. */
     297        VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
     298        rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
     299        VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
     300    }
     301
     302    return rc;
     303}
     304
     305/*
     306 * Stops and terminates the services.
     307 */
     308int VBoxServiceStopServices()
     309{
     310    int rc = 0;
     311    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     312        ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
     313    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     314        if (g_aServices[j].fStarted)
     315            g_aServices[j].pDesc->pfnStop();
     316    for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
     317    {
     318        if (g_aServices[j].Thread != NIL_RTTHREAD)
     319        {
     320            rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
     321            if (RT_FAILURE(rc))
     322                VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
     323        }
     324        g_aServices[j].pDesc->pfnTerm();
     325    }
     326
     327    return rc;
     328}
     329
    241330int main(int argc, char **argv)
    242331{
     
    279368        if(    (*psz != '-')
    280369#if defined(RT_OS_WINDOWS)
    281             && (*psz != '/')
    282 #endif
    283         )
     370            && (*psz != '/'))
     371#endif
    284372            return VBoxServiceSyntax("Unknown argument '%s'\n", psz);
    285 
    286373        psz++;
    287374
     
    441528/** @todo Make the main thread responsive to signal so it can shutdown/restart the threads on non-SIGKILL signals. */
    442529
    443     /*
    444      * Initialize the services.
    445      */
    446     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    447     {
    448         rc = g_aServices[j].pDesc->pfnInit();
    449         if (RT_FAILURE(rc))
    450             return VBoxServiceError("Service '%s' failed pre-init: %Rrc\n", g_aServices[j].pDesc->pszName);
    451     }
    452 
    453     /*
    454      * Start the service(s).
    455      */
    456     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    457     {
    458         if (    !g_aServices[j].fEnabled
    459             ||  j == iMain)
    460             continue;
    461 
    462         rc = RTThreadCreate(&g_aServices[j].Thread, VBoxServiceThread, (void *)(uintptr_t)j, 0,
    463                             RTTHREADTYPE_DEFAULT, RTTHREADFLAGS_WAITABLE, g_aServices[j].pDesc->pszName);
    464         if (RT_FAILURE(rc))
    465         {
    466             VBoxServiceError("RTThreadCreate failed, rc=%Rrc\n", rc);
    467             break;
     530    rc = VBoxServiceStartServices(iMain);
     531
     532#if defined(RT_OS_WINDOWS)
     533    if (fDaemonize && fDaemonzied)
     534    {
     535        while (1) {
     536            Sleep (100);       /** @todo */
    468537        }
    469         g_aServices[j].fStarted = true;
    470 
    471         /* wait for the thread to initialize */
    472         RTThreadUserWait(g_aServices[j].Thread, 60 * 1000);
    473         if (g_aServices[j].fShutdown)
    474             rc = VERR_GENERAL_FAILURE;
    475     }
    476     if (RT_SUCCESS(rc))
    477     {
    478         /* The final service runs in the main thread. */
    479         VBoxServiceVerbose(1, "starting '%s' in the main thread\n", g_aServices[iMain].pDesc->pszName);
    480         rc = g_aServices[iMain].pDesc->pfnWorker(&g_fShutdown);
    481         VBoxServiceError("service '%s' stopped unexpected; rc=%Rrc\n", g_aServices[iMain].pDesc->pszName, rc);
    482     }
    483 
    484     /*
    485      * Stop and terminate the services.
    486      */
    487     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    488         ASMAtomicXchgBool(&g_aServices[j].fShutdown, true);
    489     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    490         if (g_aServices[j].fStarted)
    491             g_aServices[j].pDesc->pfnStop();
    492     for (unsigned j = 0; j < RT_ELEMENTS(g_aServices); j++)
    493     {
    494         if (g_aServices[j].Thread != NIL_RTTHREAD)
    495         {
    496             rc = RTThreadWait(g_aServices[j].Thread, 30*1000, NULL);
    497             if (RT_FAILURE(rc))
    498                 VBoxServiceError("service '%s' failed to stop. (%Rrc)\n", g_aServices[j].pDesc->pszName, rc);
    499         }
    500         g_aServices[j].pDesc->pfnTerm();
    501     }
    502 
    503     return 0;
    504 }
    505 
     538    }
     539#endif
     540
     541    rc = VBoxServiceStopServices();
     542
     543#if defined(RT_OS_WINDOWS)
     544    /* Release instance mutex. */
     545    if (hMutexAppRunning != NULL) {
     546        CloseHandle (hMutexAppRunning);
     547        hMutexAppRunning = NULL;
     548    }
     549#endif
     550    return rc;
     551}
     552
  • trunk/src/VBox/Additions/common/VBoxService/VBoxServiceInternal.h

    r18642 r18670  
    111111extern void VBoxServiceVerbose(int iLevel, const char *pszFormat, ...);
    112112extern int VBoxServiceArgUInt32(int argc, char **argv, const char *psz, int *pi, uint32_t *pu32, uint32_t u32Min, uint32_t u32Max);
     113extern int VBoxServiceStartServices(int iMain);
     114extern int VBoxServiceStopServices();
    113115
    114116#if defined(RT_OS_WINDOWS)
Note: See TracChangeset for help on using the changeset viewer.

© 2025 Oracle Support Privacy / Do Not Sell My Info Terms of Use Trademark Policy Automated Access Etiquette