VirtualBox

Ignore:
Timestamp:
May 25, 2022 2:37:08 PM (3 years ago)
Author:
vboxsync
Message:

FE/VBoxAutostart: Lots of small cleanups here and there, mostly in the logging and documentation area. Added more syntax help to make more clear how to actually use this binary. Also added support for logging verbosity levels and output those stdout (if available).

Location:
trunk/src/VBox/Frontends/VBoxAutostart
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp

    r95099 r95103  
    8383static com::Bstr g_bstrServiceName;
    8484
     85/** Verbosity level. */
     86unsigned             g_cVerbosity = 0;
     87
    8588/** Logging parameters. */
    8689static uint32_t      g_cHistory = 10;                   /* Enable log rotation, 10 files. */
     
    280283            break;
    281284        case AUTOSTARTLOGTYPE_VERBOSE:
    282             if (!g_fVerbose)
     285            if (!g_cVerbosity)
    283286                return;
    284287            wType = EVENTLOG_INFORMATION_TYPE;
     
    474477     * Parse the arguments.
    475478     */
    476     bool fVerbose = false;
    477479    const char *pszUser = NULL;
    478480    static const RTGETOPTDEF s_aOptions[] =
     
    490492        {
    491493            case 'v':
    492                 fVerbose = true;
     494                g_cVerbosity++;
    493495                break;
    494496            case 'u':
     
    528530    {
    529531
    530         if (fVerbose)
     532        if (g_cVerbosity)
    531533            RTPrintf("The service %s was not installed, nothing to be done.", sServiceName.c_str());
    532534        else
     
    550552     * Parse the arguments.
    551553     */
    552     bool fVerbose = false;
    553554    const char *pszUser = NULL;
    554555    com::Utf8Str strPwd;
     
    556557    static const RTGETOPTDEF s_aOptions[] =
    557558    {
     559        /* Common options first. */
    558560        { "--verbose",       'v', RTGETOPT_REQ_NOTHING },
    559561        { "--user",          'u', RTGETOPT_REQ_STRING  },
     562        { "--username",      'u', RTGETOPT_REQ_STRING  },
    560563        { "--password-file", 'p', RTGETOPT_REQ_STRING  }
    561564    };
     
    568571        switch (ch)
    569572        {
     573            /* Common options first. */
    570574            case 'v':
    571                 fVerbose = true;
     575                g_cVerbosity++;
    572576                break;
    573577            case 'u':
     
    607611    int vrc = autostartGetDomainAndUser(pszUser, sDomain, sUserTmp);
    608612    if (RT_FAILURE(vrc))
    609         return autostartSvcDisplayError("create - CreateService failed, failed to get domain and user from string %s (%d).\n",
     613        return autostartSvcDisplayError("create - Failed to get domain and user from string '%s' (%Rrc)\n",
    610614                                        pszUser, vrc);
    611615    com::Utf8StrFmt sUserFullName("%s\\%s", sDomain.c_str(), sUserTmp.c_str());
     
    617621     * Create the service.
    618622     */
    619     RTEXITCODE rc = RTEXITCODE_FAILURE;
     623    RTEXITCODE rcExit = RTEXITCODE_FAILURE;
    620624    SC_HANDLE hSCM = autostartSvcWinOpenSCManager("create", SC_MANAGER_CREATE_SERVICE); /*SC_MANAGER_ALL_ACCESS*/
    621625    if (hSCM)
     
    624628        if (RTProcGetExecutablePath(szExecPath, sizeof(szExecPath)))
    625629        {
    626             if (fVerbose)
     630            if (g_cVerbosity)
    627631                RTPrintf("Creating the %s service, binary \"%s\"...\n",
    628632                         sServiceName.c_str(), szExecPath); /* yea, the binary name isn't UTF-8, but wtf. */
     
    657661                /** @todo Set the service description or it'll look weird in the vista service manager.
    658662                 *  Anything else that should be configured? Start access or something? */
    659                 rc = RTEXITCODE_SUCCESS;
     663                rcExit = RTEXITCODE_SUCCESS;
    660664                CloseServiceHandle(hSvc);
    661665            }
    662666            else
    663667            {
    664                 DWORD err = GetLastError();
    665                 switch (err)
     668                DWORD const dwErr = GetLastError();
     669                switch (dwErr)
    666670                {
    667671                    case ERROR_SERVICE_EXISTS:
    668                         autostartSvcDisplayError("create - The service already exists.\n");
     672                        autostartSvcDisplayError("create - The service already exists!\n");
    669673                        break;
    670674                    default:
    671                         autostartSvcDisplayError("create - CreateService failed, err=%d.\n", GetLastError());
     675                        autostartSvcDisplayError("create - CreateService failed, rc=%Rrc (%#x)\n",
     676                                                 RTErrConvertFromWin32(dwErr), dwErr);
    672677                        break;
    673678                }
     
    676681        }
    677682        else
    678             autostartSvcDisplayError("create - Failed to obtain the executable path: %d\n", GetLastError());
    679     }
    680     return rc;
     683            autostartSvcDisplayError("create - Failed to obtain the executable path\n");
     684    }
     685    return rcExit;
    681686}
    682687
     
    766771        {
    767772            if (dwControl == SERVICE_CONTROL_SHUTDOWN)
    768                 LogRel(("SERVICE_CONTROL_SHUTDOWN\n"));
     773                autostartSvcLogVerbose(1, "SERVICE_CONTROL_SHUTDOWN\n");
    769774            else
    770                 LogRel(("SERVICE_CONTROL_STOP\n"));
     775                autostartSvcLogVerbose(1, "SERVICE_CONTROL_STOP\n");
    771776
    772777            /*
     
    793798             * b/c WinSvc.h defines them in hex
    794799             */
    795             LogRel(("Unexpected service control message 0x%RX64\n",
    796                     (uint64_t)dwControl));
    797             return ERROR_CALL_NOT_IMPLEMENTED;
    798     }
    799 
    800     /* not reached */
    801 }
    802 
    803 static RTEXITCODE autostartStartVMs()
     800            autostartSvcLogWarning("Unexpected service control message 0x%RX64\n", (uint64_t)dwControl);
     801            break;
     802    }
     803
     804    return ERROR_CALL_NOT_IMPLEMENTED;
     805}
     806
     807static RTEXITCODE autostartStartVMs(void)
    804808{
    805809    int rc = autostartSetup();
     
    919923#endif
    920924
     925    DWORD dwErr = ERROR_GEN_FAILURE;
     926
    921927    /*
    922928     * Register the control handler function for the service and report to SCM.
     
    926932    if (g_hSupSvcWinCtrlHandler)
    927933    {
    928         DWORD err = ERROR_GEN_FAILURE;
    929934        if (autostartSvcWinSetServiceStatus(SERVICE_START_PENDING, 3000, NO_ERROR))
    930935        {
     
    952957                        {
    953958                            LogFlow(("autostartSvcWinServiceMain: done starting VMs\n"));
    954                             err = NO_ERROR;
     959                            dwErr = NO_ERROR;
    955960                        }
    956961                        /* No reason to keep started. Shutdown the service*/
     
    960965                else
    961966                {
    962                     err = GetLastError();
    963                     autostartSvcLogError("SetServiceStatus failed, err=%u", err);
     967                    dwErr = GetLastError();
     968                    autostartSvcLogError("SetServiceStatus failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
    964969                }
    965970
     
    972977        else
    973978        {
    974             err = GetLastError();
    975             autostartSvcLogError("SetServiceStatus failed, err=%u", err);
    976         }
    977         autostartSvcWinSetServiceStatus(SERVICE_STOPPED, 0, err);
     979            dwErr = GetLastError();
     980            autostartSvcLogError("SetServiceStatus failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
     981        }
     982        autostartSvcWinSetServiceStatus(SERVICE_STOPPED, 0, dwErr);
    978983    }
    979984    else
    980         autostartSvcLogError("RegisterServiceCtrlHandlerEx failed, err=%u", GetLastError());
     985    {
     986        dwErr = GetLastError();
     987        autostartSvcLogError("RegisterServiceCtrlHandlerEx failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
     988    }
    981989
    982990    LogFlowFuncLeave();
     
    985993
    986994/**
    987  * Handle the 'create' action.
     995 * Handle the 'runit' action.
    988996 *
    989997 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE.
     
    991999 * @param   argv    The action argument vector.
    9921000 */
    993 static int autostartSvcWinRunIt(int argc, char **argv)
     1001static RTEXITCODE autostartSvcWinRunIt(int argc, char **argv)
    9941002{
    9951003    int rc;
     
    10141022    if (FAILED(hrc))
    10151023        return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc);
    1016 
    10171024    /*
    10181025     * Initialize release logging, do this early.  This means command
     
    10451052        }
    10461053
    1047         rc = com::VBoxLogRelCreate("Autostart",
     1054        rc = com::VBoxLogRelCreate(AUTOSTART_SERVICE_NAME,
    10481055                                   szLogFile,
    10491056                                     RTLOGFLAGS_PREFIX_THREAD
     
    10661073    static const RTGETOPTDEF s_aOptions[] =
    10671074    {
     1075        /* Common options first. */
     1076        { "--verbose", 'v', RTGETOPT_REQ_NOTHING },
    10681077        { "--service", 's', RTGETOPT_REQ_STRING },
    10691078    };
     
    10711080    const char *pszServiceName = NULL;
    10721081    int ch;
    1073     RTGETOPTUNION Value;
     1082    RTGETOPTUNION ValueUnion;
    10741083    RTGETOPTSTATE GetState;
    10751084    RTGetOptInit(&GetState, argc, argv, s_aOptions, RT_ELEMENTS(s_aOptions), 0, RTGETOPTINIT_FLAGS_NO_STD_OPTS);
    1076     while ((ch = RTGetOpt(&GetState, &Value)))
     1085    while ((ch = RTGetOpt(&GetState, &ValueUnion)))
    10771086    {
    10781087        switch (ch)
    10791088        {
     1089            /* Common options first. */
     1090            case 'v':
     1091                g_cVerbosity++;
     1092                break;
    10801093            case 's':
    1081                 pszServiceName = Value.psz;
     1094                pszServiceName = ValueUnion.psz;
    10821095                try
    10831096                {
    1084                     g_bstrServiceName = com::Bstr(Value.psz);
     1097                    g_bstrServiceName = com::Bstr(ValueUnion.psz);
    10851098                }
    10861099                catch (...)
    10871100                {
    1088                     autostartSvcLogError("runit failed, service name is not valid utf-8 string or out of memory");
     1101                    autostartSvcLogError("runit failed, service name is not valid UTF-8 string or out of memory");
    10891102                    return RTEXITCODE_FAILURE;
    10901103                }
     
    10921105
    10931106            default:
    1094                 /**
    1095                  * @todo autostartSvcLogGetOptError is useless as it
    1096                  * is, should be change after RTGetOptPrintError.
    1097                  */
    1098                 return autostartSvcLogError("RTGetOpt: %Rrc\n", ch);
     1107                return autostartSvcDisplayGetOptError("runit", ch, &ValueUnion);
    10991108        }
    11001109    }
     
    11061115    }
    11071116
    1108     LogRel(("Starting service %ls\n", g_bstrServiceName.raw()));
     1117
     1118    autostartSvcLogInfo("Starting service %ls\n", g_bstrServiceName.raw());
    11091119
    11101120    /*
     
    11231133    }
    11241134
    1125     DWORD err = GetLastError();
    1126     switch (err)
     1135    DWORD const dwErr = GetLastError();
     1136    switch (dwErr)
    11271137    {
    11281138        case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT:
     
    11301140            break;
    11311141        default:
    1132             autostartSvcLogError("StartServiceCtrlDispatcher failed, err=%u", err);
     1142            autostartSvcLogError("StartServiceCtrlDispatcher failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr);
    11331143            break;
    11341144    }
     
    11371147
    11381148    return RTEXITCODE_FAILURE;
     1149}
     1150
     1151
     1152static void autostartSvcShowHeader(void)
     1153{
     1154    RTPrintf(VBOX_PRODUCT " VirtualBox Autostart Service Version " VBOX_VERSION_STRING " - r%s\n"
     1155             "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
     1156             "All rights reserved.\n\n", RTBldCfgRevisionStr());
    11391157}
    11401158
     
    11721190        RTPrintf("%s\n", VBOX_VERSION_STRING);
    11731191    else
    1174         RTPrintf("VirtualBox Autostart Service Version %s\n"
    1175                  "(C) 2012 Oracle Corporation\n"
    1176                  "All rights reserved.\n",
    1177                  VBOX_VERSION_STRING);
     1192        autostartSvcShowHeader();
    11781193    return RTEXITCODE_SUCCESS;
    11791194}
     
    11871202static RTEXITCODE autostartSvcWinShowHelp(void)
    11881203{
    1189     RTPrintf(VBOX_PRODUCT " VirtualBox Autostart Service Version " VBOX_VERSION_STRING " - r%s\n"
    1190              "(C) " VBOX_C_YEAR " " VBOX_VENDOR "\n"
    1191              "All rights reserved.\n\n", RTBldCfgRevisionStr());
     1204    autostartSvcShowHeader();
     1205
     1206    const char *pszExe = RTPathFilename(RTProcExecutablePath());
    11921207
    11931208    RTPrintf("Usage:\n"
    11941209             "\n"
    1195              "VBoxAutostartSvc\n"
    1196              "      Runs the service.\n"
    1197              "VBoxAutostartSvc <version|-v|--version> [-brief]\n"
    1198              "      Displays the version.\n"
    1199              "VBoxAutostartSvc <help|-?|-h|--help> [...]\n"
    1200              "      Displays this help screen.\n"
     1210             "%s [global-options] [command] [command-options]\n"
    12011211             "\n"
    1202              "VBoxAutostartSvc <install|/RegServer|/i>\n"
    1203              "      Installs the service.\n"
    1204              "VBoxAutostartSvc <uninstall|delete|/UnregServer|/u>\n"
    1205              "      Uninstalls the service.\n"
    1206              );
     1212             "Global options:\n"
     1213             "  <help|-?|-h|--help> [...]\n"
     1214             "    Displays this help screen.\n"
     1215             "  <version|-v|--version> [-brief]\n"
     1216             "    Displays the version.\n"
     1217             "\n"
     1218             "No command given:\n"
     1219             "  Runs the service.\n"
     1220             "  --service <name>\n"
     1221             "    Specifies the service name to run.\n"
     1222             "\n"
     1223             "Command </i|install|/RegServer> --user <username> --password-file <...>\n"
     1224             "  Installs the service.\n"
     1225             "Options:\n"
     1226             "  --user <username>\n"
     1227             "    Specifies the user name the service should use for installation.\n"
     1228             "  --password-file <path/to/file>\n"
     1229             "    Specifies the file for user password to use for installation.\n"
     1230             "\n"
     1231             "Command </u|uninstall|delete|/UnregServer>\n"
     1232             "  Uninstalls the service.\n",
     1233             pszExe);
    12071234    return RTEXITCODE_SUCCESS;
    12081235}
  • trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h

    r93115 r95103  
    198198 * Logs a verbose message to the appropriate system log.
    199199 *
    200  * @param   pszFormat   The log string. No trailing newline.
    201  * @param   ...         Format arguments.
    202  */
    203 DECLHIDDEN(void) autostartSvcLogVerboseV(const char *pszFormat, va_list va);
     200 * @param   cVerbosity  Verbosity level when logging should happen.
     201 * @param   pszFormat   The log string. No trailing newline.
     202 * @param   ...         Format arguments.
     203 */
     204DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va);
    204205
    205206/**
    206207 * Logs a verbose message to the appropriate system log.
    207208 *
    208  * @param   pszFormat   The log string. No trailing newline.
    209  * @param   ...         Format arguments.
    210  */
    211 DECLHIDDEN(void) autostartSvcLogVerbose(const char *pszFormat, ...);
     209 * @param   cVerbosity  Verbosity level when logging should happen.
     210 * @param   pszFormat   The log string. No trailing newline.
     211 * @param   ...         Format arguments.
     212 */
     213DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...);
    212214
    213215/**
  • trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp

    r93115 r95103  
    3333
    3434#include "VBoxAutostart.h"
     35
     36extern unsigned g_cVerbosity;
    3537
    3638using namespace com;
     
    7274    if (uStartupDelay)
    7375    {
    74         autostartSvcLogVerbose("Delay starting for %d seconds ...\n", uStartupDelay);
     76        autostartSvcLogVerbose(1, "Delaying start for %RU32 seconds ...\n", uStartupDelay);
    7577        vrc = RTThreadSleep(uStartupDelay * 1000);
    7678    }
     
    137139                if ((*it).uStartupDelay > uDelayCurr)
    138140                {
    139                     autostartSvcLogVerbose("Delay starting of the next VMs for %d seconds ...\n",
     141                    autostartSvcLogVerbose(1, "Delaying start of the next VMs for %ul seconds ...\n",
    140142                                           (*it).uStartupDelay - uDelayCurr);
    141143                    RTThreadSleep(((*it).uStartupDelay - uDelayCurr) * 1000);
     
    150152                if (SUCCEEDED(rc) && !progress.isNull())
    151153                {
    152                     autostartSvcLogVerbose("Waiting for VM \"%ls\" to power on...\n", (*it).strId.raw());
     154                    autostartSvcLogVerbose(1, "Waiting for VM '%ls' to power on...\n", (*it).strId.raw());
    153155                    CHECK_ERROR(progress, WaitForCompletion(-1));
    154156                    if (SUCCEEDED(rc))
     
    170172                                }
    171173                                else
    172                                     autostartSvcLogVerbose("VM \"%ls\" has been successfully started.\n", (*it).strId.raw());
     174                                    autostartSvcLogVerbose(1, "VM '%ls' has been successfully started.\n", (*it).strId.raw());
    173175                            }
    174176                        }
  • trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp

    r93115 r95103  
    3535
    3636using namespace com;
     37
     38extern unsigned g_cVerbosity;
    3739
    3840DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
     
    117119}
    118120
    119 DECLHIDDEN(void) autostartSvcLogVerboseV(const char *pszFormat, va_list va)
    120 {
    121     if (*pszFormat)
    122     {
    123         char *pszMsg = NULL;
    124         if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    125         {
    126             autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
    127             RTStrFree(pszMsg);
    128         }
    129         else
    130             autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_VERBOSE);
    131     }
    132 }
    133 
    134 DECLHIDDEN(void) autostartSvcLogVerbose(const char *pszFormat, ...)
    135 {
    136     va_list va;
    137     va_start(va, pszFormat);
    138     autostartSvcLogVerboseV(pszFormat, va);
     121DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va)
     122{
     123    AssertPtrReturnVoid(pszFormat);
     124
     125    if (g_cVerbosity < cVerbosity)
     126       return;
     127
     128    char *pszMsg = NULL;
     129    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
     130    {
     131        RTPrintf("%s", pszMsg);
     132        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
     133        RTStrFree(pszMsg);
     134    }
     135}
     136
     137DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...)
     138{
     139    va_list va;
     140    va_start(va, pszFormat);
     141    autostartSvcLogVerboseV(cVerbosity, pszFormat, va);
    139142    va_end(va);
    140143}
     
    142145DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
    143146{
    144     if (*pszFormat)
    145     {
    146         char *pszMsg = NULL;
    147         if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    148         {
    149             autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
    150             RTStrFree(pszMsg);
    151         }
    152         else
    153             autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_WARNING);
    154     }
     147    AssertPtrReturnVoid(pszFormat);
     148
     149    char *pszMsg = NULL;
     150    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
     151    {
     152        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
     153        RTStrFree(pszMsg);
     154    }
     155}
     156
     157DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
     158{
     159    va_list va;
     160    va_start(va, pszFormat);
     161    autostartSvcLogWarningV(pszFormat, va);
     162    va_end(va);
    155163}
    156164
     
    165173DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
    166174{
    167     if (*pszFormat)
    168     {
    169         char *pszMsg = NULL;
    170         if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    171         {
    172             autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
    173             RTStrFree(pszMsg);
    174         }
    175         else
    176             autostartSvcOsLogStr(pszFormat, AUTOSTARTLOGTYPE_INFO);
    177     }
    178 }
    179 
    180 DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
    181 {
    182     va_list va;
    183     va_start(va, pszFormat);
    184     autostartSvcLogWarningV(pszFormat, va);
    185     va_end(va);
     175    AssertPtrReturnVoid(pszFormat);
     176
     177    char *pszMsg = NULL;
     178    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
     179    {
     180        RTPrintf("%s", pszMsg);
     181        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
     182        RTStrFree(pszMsg);
     183    }
    186184}
    187185
     
    205203DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
    206204{
    207     RTStrmPrintf(g_pStdErr, "VBoxSupSvc error: ");
     205    RTStrmPrintf(g_pStdErr, "Error: ");
    208206    RTStrmPrintfV(g_pStdErr, pszFormat, va);
    209207    Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
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