VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp@ 106042

Last change on this file since 106042 was 103858, checked in by vboxsync, 9 months ago

FE/VBoxAutostart: Made it easier to use on Windows by printing out the syntax help if an invalid (or no) command is being specified. Fixed missing newlines for error output.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: VBoxAutostartUtils.cpp 103858 2024-03-14 17:01:42Z vboxsync $ */
2/** @file
3 * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
4 * Utils used by the windows and posix frontends.
5 */
6
7/*
8 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
9 *
10 * This file is part of VirtualBox base platform packages, as
11 * available from https://www.virtualbox.org.
12 *
13 * This program is free software; you can redistribute it and/or
14 * modify it under the terms of the GNU General Public License
15 * as published by the Free Software Foundation, in version 3 of the
16 * License.
17 *
18 * This program is distributed in the hope that it will be useful, but
19 * WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
21 * General Public License for more details.
22 *
23 * You should have received a copy of the GNU General Public License
24 * along with this program; if not, see <https://www.gnu.org/licenses>.
25 *
26 * SPDX-License-Identifier: GPL-3.0-only
27 */
28
29#include <VBox/com/com.h>
30#include <VBox/com/string.h>
31#include <VBox/com/Guid.h>
32#include <VBox/com/array.h>
33#include <VBox/com/ErrorInfo.h>
34#include <VBox/com/errorprint.h>
35
36#include <VBox/err.h>
37#include <VBox/version.h>
38
39#include <iprt/buildconfig.h>
40#include <iprt/message.h>
41#include <iprt/thread.h>
42#include <iprt/stream.h>
43#include <iprt/log.h>
44#include <iprt/path.h> /* RTPATH_MAX */
45
46#include "VBoxAutostart.h"
47
48using namespace com;
49
50extern unsigned g_cVerbosity;
51
52DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
53{
54 switch (machineState)
55 {
56 case MachineState_PoweredOff:
57 return fShort ? "poweroff" : "powered off";
58 case MachineState_Saved:
59 return "saved";
60 case MachineState_Teleported:
61 return "teleported";
62 case MachineState_Aborted:
63 return "aborted";
64 case MachineState_AbortedSaved:
65 return "aborted-saved";
66 case MachineState_Running:
67 return "running";
68 case MachineState_Paused:
69 return "paused";
70 case MachineState_Stuck:
71 return fShort ? "gurumeditation" : "guru meditation";
72 case MachineState_Teleporting:
73 return "teleporting";
74 case MachineState_LiveSnapshotting:
75 return fShort ? "livesnapshotting" : "live snapshotting";
76 case MachineState_Starting:
77 return "starting";
78 case MachineState_Stopping:
79 return "stopping";
80 case MachineState_Saving:
81 return "saving";
82 case MachineState_Restoring:
83 return "restoring";
84 case MachineState_TeleportingPausedVM:
85 return fShort ? "teleportingpausedvm" : "teleporting paused vm";
86 case MachineState_TeleportingIn:
87 return fShort ? "teleportingin" : "teleporting (incoming)";
88 case MachineState_DeletingSnapshotOnline:
89 return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
90 case MachineState_DeletingSnapshotPaused:
91 return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
92 case MachineState_OnlineSnapshotting:
93 return fShort ? "onlinesnapshotting" : "online snapshotting";
94 case MachineState_RestoringSnapshot:
95 return fShort ? "restoringsnapshot" : "restoring snapshot";
96 case MachineState_DeletingSnapshot:
97 return fShort ? "deletingsnapshot" : "deleting snapshot";
98 case MachineState_SettingUp:
99 return fShort ? "settingup" : "setting up";
100 case MachineState_Snapshotting:
101 return "snapshotting";
102 default:
103 break;
104 }
105 return "unknown";
106}
107
108DECLHIDDEN(void) autostartSvcShowHeader(void)
109{
110 RTPrintf(VBOX_PRODUCT " VirtualBox Autostart Service Version " VBOX_VERSION_STRING " - r%s\n"
111 "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n", RTBldCfgRevisionStr());
112}
113
114DECLHIDDEN(void) autostartSvcShowVersion(bool fBrief)
115{
116 if (fBrief)
117 RTPrintf("%s\n", VBOX_VERSION_STRING);
118 else
119 autostartSvcShowHeader();
120}
121
122DECLHIDDEN(int) autostartSvcLogErrorV(const char *pszFormat, va_list va)
123{
124 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
125
126 char *pszMsg = NULL;
127 if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
128 {
129 autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
130 RTStrFree(pszMsg);
131 return VINF_SUCCESS;
132 }
133
134 return VERR_BUFFER_OVERFLOW;
135}
136
137DECLHIDDEN(int) autostartSvcLogError(const char *pszFormat, ...)
138{
139 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
140
141 va_list va;
142 va_start(va, pszFormat);
143 int rc = autostartSvcLogErrorV(pszFormat, va);
144 va_end(va);
145
146 return rc;
147}
148
149DECLHIDDEN(int) autostartSvcLogErrorRcV(int rc, const char *pszFormat, va_list va)
150{
151 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
152
153 int rc2 = autostartSvcLogErrorV(pszFormat, va);
154 if (RT_SUCCESS(rc2))
155 return rc; /* Return handed-in rc. */
156 return rc2;
157}
158
159DECLHIDDEN(int) autostartSvcLogErrorRc(int rc, const char *pszFormat, ...)
160{
161 AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);
162
163 va_list va;
164 va_start(va, pszFormat);
165 int rc2 = autostartSvcLogErrorRcV(rc, pszFormat, va);
166 va_end(va);
167 return rc2;
168}
169
170DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va)
171{
172 AssertPtrReturnVoid(pszFormat);
173
174 if (g_cVerbosity < cVerbosity)
175 return;
176
177 char *pszMsg = NULL;
178 if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
179 {
180 autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
181 RTStrFree(pszMsg);
182 }
183}
184
185DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...)
186{
187 va_list va;
188 va_start(va, pszFormat);
189 autostartSvcLogVerboseV(cVerbosity, pszFormat, va);
190 va_end(va);
191}
192
193DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
194{
195 AssertPtrReturnVoid(pszFormat);
196
197 char *pszMsg = NULL;
198 if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
199 {
200 autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
201 RTStrFree(pszMsg);
202 }
203}
204
205DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
206{
207 va_list va;
208 va_start(va, pszFormat);
209 autostartSvcLogWarningV(pszFormat, va);
210 va_end(va);
211}
212
213DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
214{
215 va_list va;
216 va_start(va, pszFormat);
217 autostartSvcLogInfoV(pszFormat, va);
218 va_end(va);
219}
220
221DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
222{
223 AssertPtrReturnVoid(pszFormat);
224
225 char *pszMsg = NULL;
226 if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
227 {
228 autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
229 RTStrFree(pszMsg);
230 }
231}
232
233DECLHIDDEN(int) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
234{
235 RT_NOREF(pValue);
236 autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s\n", pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
237 return RTEXITCODE_SYNTAX;
238}
239
240DECLHIDDEN(int) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
241{
242 AssertReturn(iArg < argc, RTEXITCODE_FAILURE);
243 autostartSvcLogError("%s - Too many arguments: %s\n", pszAction, argv[iArg]);
244 for ( ; iArg < argc; iArg++)
245 LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
246 return VERR_INVALID_PARAMETER;
247}
248
249DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
250{
251 RTStrmPrintf(g_pStdErr, "Error: ");
252 RTStrmPrintfV(g_pStdErr, pszFormat, va);
253 Log(("autostartSvcDisplayErrorV: %s\n", pszFormat)); /** @todo format it! */
254 return RTEXITCODE_FAILURE;
255}
256
257DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...)
258{
259 va_list va;
260 va_start(va, pszFormat);
261 autostartSvcDisplayErrorV(pszFormat, va);
262 va_end(va);
263 return RTEXITCODE_FAILURE;
264}
265
266DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue)
267{
268 char szMsg[4096];
269 RTGetOptFormatError(szMsg, sizeof(szMsg), rc, pValue);
270 autostartSvcDisplayError("%s - %s\n", pszAction, szMsg);
271 return RTEXITCODE_SYNTAX;
272}
273
274DECLHIDDEN(int) autostartSetup(void)
275{
276 autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);
277
278 /*
279 * Initialize COM.
280 */
281 using namespace com;
282 HRESULT hrc = com::Initialize();
283# ifdef VBOX_WITH_XPCOM
284 if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
285 {
286 char szHome[RTPATH_MAX] = "";
287 com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
288 autostartSvcLogError("Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
289 return VERR_COM_FILE_ERROR;
290 }
291# endif
292 if (FAILED(hrc))
293 {
294 autostartSvcLogError("Failed to initialize COM (%Rhrc)!", hrc);
295 return VERR_COM_UNEXPECTED;
296 }
297
298 hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
299 if (FAILED(hrc))
300 {
301 RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
302 com::ErrorInfo info;
303 if (!info.isFullAvailable() && !info.isBasicAvailable())
304 {
305 com::GluePrintRCMessage(hrc);
306 autostartSvcLogError("Most likely, the VirtualBox COM server is not running or failed to start.");
307 }
308 else
309 com::GluePrintErrorInfo(info);
310 return VERR_COM_UNEXPECTED;
311 }
312
313 /*
314 * Setup VirtualBox + session interfaces.
315 */
316 hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
317 if (SUCCEEDED(hrc))
318 {
319 hrc = g_pSession.createInprocObject(CLSID_Session);
320 if (FAILED(hrc))
321 autostartSvcLogError("Failed to create a session object (rc=%Rhrc)!", hrc);
322 }
323 else
324 autostartSvcLogError("Failed to get VirtualBox object (rc=%Rhrc)!", hrc);
325
326 if (FAILED(hrc))
327 return VERR_COM_OBJECT_NOT_FOUND;
328
329 return VINF_SUCCESS;
330}
331
332DECLHIDDEN(void) autostartShutdown(void)
333{
334 autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);
335
336 g_pSession.setNull();
337 g_pVirtualBox.setNull();
338 g_pVirtualBoxClient.setNull();
339 com::Shutdown();
340}
341
Note: See TracBrowser for help on using the repository browser.

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