Changeset 95103 in vbox for trunk/src/VBox/Frontends/VBoxAutostart
- Timestamp:
- May 25, 2022 2:37:08 PM (3 years ago)
- Location:
- trunk/src/VBox/Frontends/VBoxAutostart
- Files:
-
- 4 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostart-win.cpp
r95099 r95103 83 83 static com::Bstr g_bstrServiceName; 84 84 85 /** Verbosity level. */ 86 unsigned g_cVerbosity = 0; 87 85 88 /** Logging parameters. */ 86 89 static uint32_t g_cHistory = 10; /* Enable log rotation, 10 files. */ … … 280 283 break; 281 284 case AUTOSTARTLOGTYPE_VERBOSE: 282 if (!g_ fVerbose)285 if (!g_cVerbosity) 283 286 return; 284 287 wType = EVENTLOG_INFORMATION_TYPE; … … 474 477 * Parse the arguments. 475 478 */ 476 bool fVerbose = false;477 479 const char *pszUser = NULL; 478 480 static const RTGETOPTDEF s_aOptions[] = … … 490 492 { 491 493 case 'v': 492 fVerbose = true;494 g_cVerbosity++; 493 495 break; 494 496 case 'u': … … 528 530 { 529 531 530 if ( fVerbose)532 if (g_cVerbosity) 531 533 RTPrintf("The service %s was not installed, nothing to be done.", sServiceName.c_str()); 532 534 else … … 550 552 * Parse the arguments. 551 553 */ 552 bool fVerbose = false;553 554 const char *pszUser = NULL; 554 555 com::Utf8Str strPwd; … … 556 557 static const RTGETOPTDEF s_aOptions[] = 557 558 { 559 /* Common options first. */ 558 560 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 559 561 { "--user", 'u', RTGETOPT_REQ_STRING }, 562 { "--username", 'u', RTGETOPT_REQ_STRING }, 560 563 { "--password-file", 'p', RTGETOPT_REQ_STRING } 561 564 }; … … 568 571 switch (ch) 569 572 { 573 /* Common options first. */ 570 574 case 'v': 571 fVerbose = true;575 g_cVerbosity++; 572 576 break; 573 577 case 'u': … … 607 611 int vrc = autostartGetDomainAndUser(pszUser, sDomain, sUserTmp); 608 612 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", 610 614 pszUser, vrc); 611 615 com::Utf8StrFmt sUserFullName("%s\\%s", sDomain.c_str(), sUserTmp.c_str()); … … 617 621 * Create the service. 618 622 */ 619 RTEXITCODE rc = RTEXITCODE_FAILURE;623 RTEXITCODE rcExit = RTEXITCODE_FAILURE; 620 624 SC_HANDLE hSCM = autostartSvcWinOpenSCManager("create", SC_MANAGER_CREATE_SERVICE); /*SC_MANAGER_ALL_ACCESS*/ 621 625 if (hSCM) … … 624 628 if (RTProcGetExecutablePath(szExecPath, sizeof(szExecPath))) 625 629 { 626 if ( fVerbose)630 if (g_cVerbosity) 627 631 RTPrintf("Creating the %s service, binary \"%s\"...\n", 628 632 sServiceName.c_str(), szExecPath); /* yea, the binary name isn't UTF-8, but wtf. */ … … 657 661 /** @todo Set the service description or it'll look weird in the vista service manager. 658 662 * Anything else that should be configured? Start access or something? */ 659 rc = RTEXITCODE_SUCCESS;663 rcExit = RTEXITCODE_SUCCESS; 660 664 CloseServiceHandle(hSvc); 661 665 } 662 666 else 663 667 { 664 DWORD err = GetLastError();665 switch ( err)668 DWORD const dwErr = GetLastError(); 669 switch (dwErr) 666 670 { 667 671 case ERROR_SERVICE_EXISTS: 668 autostartSvcDisplayError("create - The service already exists .\n");672 autostartSvcDisplayError("create - The service already exists!\n"); 669 673 break; 670 674 default: 671 autostartSvcDisplayError("create - CreateService failed, err=%d.\n", GetLastError()); 675 autostartSvcDisplayError("create - CreateService failed, rc=%Rrc (%#x)\n", 676 RTErrConvertFromWin32(dwErr), dwErr); 672 677 break; 673 678 } … … 676 681 } 677 682 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; 681 686 } 682 687 … … 766 771 { 767 772 if (dwControl == SERVICE_CONTROL_SHUTDOWN) 768 LogRel(("SERVICE_CONTROL_SHUTDOWN\n"));773 autostartSvcLogVerbose(1, "SERVICE_CONTROL_SHUTDOWN\n"); 769 774 else 770 LogRel(("SERVICE_CONTROL_STOP\n"));775 autostartSvcLogVerbose(1, "SERVICE_CONTROL_STOP\n"); 771 776 772 777 /* … … 793 798 * b/c WinSvc.h defines them in hex 794 799 */ 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 807 static RTEXITCODE autostartStartVMs(void) 804 808 { 805 809 int rc = autostartSetup(); … … 919 923 #endif 920 924 925 DWORD dwErr = ERROR_GEN_FAILURE; 926 921 927 /* 922 928 * Register the control handler function for the service and report to SCM. … … 926 932 if (g_hSupSvcWinCtrlHandler) 927 933 { 928 DWORD err = ERROR_GEN_FAILURE;929 934 if (autostartSvcWinSetServiceStatus(SERVICE_START_PENDING, 3000, NO_ERROR)) 930 935 { … … 952 957 { 953 958 LogFlow(("autostartSvcWinServiceMain: done starting VMs\n")); 954 err = NO_ERROR;959 dwErr = NO_ERROR; 955 960 } 956 961 /* No reason to keep started. Shutdown the service*/ … … 960 965 else 961 966 { 962 err = GetLastError();963 autostartSvcLogError("SetServiceStatus failed, err=%u", err);967 dwErr = GetLastError(); 968 autostartSvcLogError("SetServiceStatus failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr); 964 969 } 965 970 … … 972 977 else 973 978 { 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); 978 983 } 979 984 else 980 autostartSvcLogError("RegisterServiceCtrlHandlerEx failed, err=%u", GetLastError()); 985 { 986 dwErr = GetLastError(); 987 autostartSvcLogError("RegisterServiceCtrlHandlerEx failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr); 988 } 981 989 982 990 LogFlowFuncLeave(); … … 985 993 986 994 /** 987 * Handle the ' create' action.995 * Handle the 'runit' action. 988 996 * 989 997 * @returns RTEXITCODE_SUCCESS or RTEXITCODE_FAILURE. … … 991 999 * @param argv The action argument vector. 992 1000 */ 993 static intautostartSvcWinRunIt(int argc, char **argv)1001 static RTEXITCODE autostartSvcWinRunIt(int argc, char **argv) 994 1002 { 995 1003 int rc; … … 1014 1022 if (FAILED(hrc)) 1015 1023 return RTMsgErrorExit(RTEXITCODE_FAILURE, "Failed to initialize COM (%Rhrc)!", hrc); 1016 1017 1024 /* 1018 1025 * Initialize release logging, do this early. This means command … … 1045 1052 } 1046 1053 1047 rc = com::VBoxLogRelCreate( "Autostart",1054 rc = com::VBoxLogRelCreate(AUTOSTART_SERVICE_NAME, 1048 1055 szLogFile, 1049 1056 RTLOGFLAGS_PREFIX_THREAD … … 1066 1073 static const RTGETOPTDEF s_aOptions[] = 1067 1074 { 1075 /* Common options first. */ 1076 { "--verbose", 'v', RTGETOPT_REQ_NOTHING }, 1068 1077 { "--service", 's', RTGETOPT_REQ_STRING }, 1069 1078 }; … … 1071 1080 const char *pszServiceName = NULL; 1072 1081 int ch; 1073 RTGETOPTUNION Value ;1082 RTGETOPTUNION ValueUnion; 1074 1083 RTGETOPTSTATE GetState; 1075 1084 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))) 1077 1086 { 1078 1087 switch (ch) 1079 1088 { 1089 /* Common options first. */ 1090 case 'v': 1091 g_cVerbosity++; 1092 break; 1080 1093 case 's': 1081 pszServiceName = Value .psz;1094 pszServiceName = ValueUnion.psz; 1082 1095 try 1083 1096 { 1084 g_bstrServiceName = com::Bstr(Value .psz);1097 g_bstrServiceName = com::Bstr(ValueUnion.psz); 1085 1098 } 1086 1099 catch (...) 1087 1100 { 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"); 1089 1102 return RTEXITCODE_FAILURE; 1090 1103 } … … 1092 1105 1093 1106 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); 1099 1108 } 1100 1109 } … … 1106 1115 } 1107 1116 1108 LogRel(("Starting service %ls\n", g_bstrServiceName.raw())); 1117 1118 autostartSvcLogInfo("Starting service %ls\n", g_bstrServiceName.raw()); 1109 1119 1110 1120 /* … … 1123 1133 } 1124 1134 1125 DWORD err = GetLastError();1126 switch ( err)1135 DWORD const dwErr = GetLastError(); 1136 switch (dwErr) 1127 1137 { 1128 1138 case ERROR_FAILED_SERVICE_CONTROLLER_CONNECT: … … 1130 1140 break; 1131 1141 default: 1132 autostartSvcLogError("StartServiceCtrlDispatcher failed, err=%u", err);1142 autostartSvcLogError("StartServiceCtrlDispatcher failed, rc=%Rrc (%#x)\n", RTErrConvertFromWin32(dwErr), dwErr); 1133 1143 break; 1134 1144 } … … 1137 1147 1138 1148 return RTEXITCODE_FAILURE; 1149 } 1150 1151 1152 static 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()); 1139 1157 } 1140 1158 … … 1172 1190 RTPrintf("%s\n", VBOX_VERSION_STRING); 1173 1191 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(); 1178 1193 return RTEXITCODE_SUCCESS; 1179 1194 } … … 1187 1202 static RTEXITCODE autostartSvcWinShowHelp(void) 1188 1203 { 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()); 1192 1207 1193 1208 RTPrintf("Usage:\n" 1194 1209 "\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" 1201 1211 "\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); 1207 1234 return RTEXITCODE_SUCCESS; 1208 1235 } -
trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostart.h
r93115 r95103 198 198 * Logs a verbose message to the appropriate system log. 199 199 * 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 */ 204 DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va); 204 205 205 206 /** 206 207 * Logs a verbose message to the appropriate system log. 207 208 * 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 */ 213 DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...); 212 214 213 215 /** -
trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp
r93115 r95103 33 33 34 34 #include "VBoxAutostart.h" 35 36 extern unsigned g_cVerbosity; 35 37 36 38 using namespace com; … … 72 74 if (uStartupDelay) 73 75 { 74 autostartSvcLogVerbose( "Delay starting for %dseconds ...\n", uStartupDelay);76 autostartSvcLogVerbose(1, "Delaying start for %RU32 seconds ...\n", uStartupDelay); 75 77 vrc = RTThreadSleep(uStartupDelay * 1000); 76 78 } … … 137 139 if ((*it).uStartupDelay > uDelayCurr) 138 140 { 139 autostartSvcLogVerbose( "Delay starting of the next VMs for %dseconds ...\n",141 autostartSvcLogVerbose(1, "Delaying start of the next VMs for %ul seconds ...\n", 140 142 (*it).uStartupDelay - uDelayCurr); 141 143 RTThreadSleep(((*it).uStartupDelay - uDelayCurr) * 1000); … … 150 152 if (SUCCEEDED(rc) && !progress.isNull()) 151 153 { 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()); 153 155 CHECK_ERROR(progress, WaitForCompletion(-1)); 154 156 if (SUCCEEDED(rc)) … … 170 172 } 171 173 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()); 173 175 } 174 176 } -
trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp
r93115 r95103 35 35 36 36 using namespace com; 37 38 extern unsigned g_cVerbosity; 37 39 38 40 DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort) … … 117 119 } 118 120 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); 121 DECLHIDDEN(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 137 DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...) 138 { 139 va_list va; 140 va_start(va, pszFormat); 141 autostartSvcLogVerboseV(cVerbosity, pszFormat, va); 139 142 va_end(va); 140 143 } … … 142 145 DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va) 143 146 { 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 157 DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...) 158 { 159 va_list va; 160 va_start(va, pszFormat); 161 autostartSvcLogWarningV(pszFormat, va); 162 va_end(va); 155 163 } 156 164 … … 165 173 DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va) 166 174 { 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 } 186 184 } 187 185 … … 205 203 DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va) 206 204 { 207 RTStrmPrintf(g_pStdErr, " VBoxSupSvc error: ");205 RTStrmPrintf(g_pStdErr, "Error: "); 208 206 RTStrmPrintfV(g_pStdErr, pszFormat, va); 209 207 Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
Note:
See TracChangeset
for help on using the changeset viewer.