summaryrefslogtreecommitdiff
path: root/src/VBox/Frontends/VBoxAutostart/VBoxAutostartUtils.cpp
blob: 1f141701136da013dfa37b0a02c59edd3b2c8ea3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
/* $Id$ */
/** @file
 * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
 *                 Utils used by the windows and posix frontends.
 */

/*
 * Copyright (C) 2012-2022 Oracle and/or its affiliates.
 *
 * This file is part of VirtualBox base platform packages, as
 * available from https://www.virtualbox.org.
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation, in version 3 of the
 * License.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <https://www.gnu.org/licenses>.
 *
 * SPDX-License-Identifier: GPL-3.0-only
 */

#include <VBox/com/com.h>
#include <VBox/com/string.h>
#include <VBox/com/Guid.h>
#include <VBox/com/array.h>
#include <VBox/com/ErrorInfo.h>
#include <VBox/com/errorprint.h>

#include <VBox/err.h>
#include <VBox/version.h>

#include <iprt/buildconfig.h>
#include <iprt/message.h>
#include <iprt/thread.h>
#include <iprt/stream.h>
#include <iprt/log.h>
#include <iprt/path.h>      /* RTPATH_MAX */

#include "VBoxAutostart.h"

using namespace com;

extern unsigned g_cVerbosity;

DECLHIDDEN(const char *) machineStateToName(MachineState_T machineState, bool fShort)
{
    switch (machineState)
    {
        case MachineState_PoweredOff:
            return fShort ? "poweroff"             : "powered off";
        case MachineState_Saved:
            return "saved";
        case MachineState_Teleported:
            return "teleported";
        case MachineState_Aborted:
            return "aborted";
        case MachineState_AbortedSaved:
            return "aborted-saved";
        case MachineState_Running:
            return "running";
        case MachineState_Paused:
            return "paused";
        case MachineState_Stuck:
            return fShort ? "gurumeditation"       : "guru meditation";
        case MachineState_Teleporting:
            return "teleporting";
        case MachineState_LiveSnapshotting:
            return fShort ? "livesnapshotting"     : "live snapshotting";
        case MachineState_Starting:
            return "starting";
        case MachineState_Stopping:
            return "stopping";
        case MachineState_Saving:
            return "saving";
        case MachineState_Restoring:
            return "restoring";
        case MachineState_TeleportingPausedVM:
            return fShort ? "teleportingpausedvm"  : "teleporting paused vm";
        case MachineState_TeleportingIn:
            return fShort ? "teleportingin"        : "teleporting (incoming)";
        case MachineState_DeletingSnapshotOnline:
            return fShort ? "deletingsnapshotlive" : "deleting snapshot live";
        case MachineState_DeletingSnapshotPaused:
            return fShort ? "deletingsnapshotlivepaused" : "deleting snapshot live paused";
        case MachineState_OnlineSnapshotting:
            return fShort ? "onlinesnapshotting"   : "online snapshotting";
        case MachineState_RestoringSnapshot:
            return fShort ? "restoringsnapshot"    : "restoring snapshot";
        case MachineState_DeletingSnapshot:
            return fShort ? "deletingsnapshot"     : "deleting snapshot";
        case MachineState_SettingUp:
            return fShort ? "settingup"           : "setting up";
        case MachineState_Snapshotting:
            return "snapshotting";
        default:
            break;
    }
    return "unknown";
}

DECLHIDDEN(void) autostartSvcShowHeader(void)
{
    RTPrintf(VBOX_PRODUCT " VirtualBox Autostart Service Version " VBOX_VERSION_STRING " - r%s\n"
             "Copyright (C) " VBOX_C_YEAR " " VBOX_VENDOR "\n\n", RTBldCfgRevisionStr());
}

DECLHIDDEN(void) autostartSvcShowVersion(bool fBrief)
{
    if (fBrief)
        RTPrintf("%s\n", VBOX_VERSION_STRING);
    else
        autostartSvcShowHeader();
}

DECLHIDDEN(int) autostartSvcLogErrorV(const char *pszFormat, va_list va)
{
    AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);

    char *pszMsg = NULL;
    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    {
        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_ERROR);
        RTStrFree(pszMsg);
        return VINF_SUCCESS;
    }

    return VERR_BUFFER_OVERFLOW;
}

DECLHIDDEN(int) autostartSvcLogError(const char *pszFormat, ...)
{
    AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);

    va_list va;
    va_start(va, pszFormat);
    int rc = autostartSvcLogErrorV(pszFormat, va);
    va_end(va);

    return rc;
}

DECLHIDDEN(int) autostartSvcLogErrorRcV(int rc, const char *pszFormat, va_list va)
{
    AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);

    int rc2 = autostartSvcLogErrorV(pszFormat, va);
    if (RT_SUCCESS(rc2))
        return rc; /* Return handed-in rc. */
    return rc2;
}

DECLHIDDEN(int) autostartSvcLogErrorRc(int rc, const char *pszFormat, ...)
{
    AssertPtrReturn(pszFormat, VERR_INVALID_POINTER);

    va_list va;
    va_start(va, pszFormat);
    int rc2 = autostartSvcLogErrorRcV(rc, pszFormat, va);
    va_end(va);
    return rc2;
}

DECLHIDDEN(void) autostartSvcLogVerboseV(unsigned cVerbosity, const char *pszFormat, va_list va)
{
    AssertPtrReturnVoid(pszFormat);

    if (g_cVerbosity < cVerbosity)
       return;

    char *pszMsg = NULL;
    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    {
        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_VERBOSE);
        RTStrFree(pszMsg);
    }
}

DECLHIDDEN(void) autostartSvcLogVerbose(unsigned cVerbosity, const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    autostartSvcLogVerboseV(cVerbosity, pszFormat, va);
    va_end(va);
}

DECLHIDDEN(void) autostartSvcLogWarningV(const char *pszFormat, va_list va)
{
    AssertPtrReturnVoid(pszFormat);

    char *pszMsg = NULL;
    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    {
        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_WARNING);
        RTStrFree(pszMsg);
    }
}

DECLHIDDEN(void) autostartSvcLogWarning(const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    autostartSvcLogWarningV(pszFormat, va);
    va_end(va);
}

DECLHIDDEN(void) autostartSvcLogInfo(const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    autostartSvcLogInfoV(pszFormat, va);
    va_end(va);
}

DECLHIDDEN(void) autostartSvcLogInfoV(const char *pszFormat, va_list va)
{
    AssertPtrReturnVoid(pszFormat);

    char *pszMsg = NULL;
    if (RTStrAPrintfV(&pszMsg, pszFormat, va) != -1)
    {
        autostartSvcOsLogStr(pszMsg, AUTOSTARTLOGTYPE_INFO);
        RTStrFree(pszMsg);
    }
}

DECLHIDDEN(int) autostartSvcLogGetOptError(const char *pszAction, int rc, int argc, char **argv, int iArg, PCRTGETOPTUNION pValue)
{
    RT_NOREF(pValue);
    autostartSvcLogError("%s - RTGetOpt failure, %Rrc (%d): %s", pszAction, rc, rc, iArg < argc ? argv[iArg] : "<null>");
    return RTEXITCODE_SYNTAX;
}

DECLHIDDEN(int) autostartSvcLogTooManyArgsError(const char *pszAction, int argc, char **argv, int iArg)
{
    AssertReturn(iArg < argc, RTEXITCODE_FAILURE);
    autostartSvcLogError("%s - Too many arguments: %s", pszAction, argv[iArg]);
    for ( ; iArg < argc; iArg++)
        LogRel(("arg#%i: %s\n", iArg, argv[iArg]));
    return VERR_INVALID_PARAMETER;
}

DECLHIDDEN(RTEXITCODE) autostartSvcDisplayErrorV(const char *pszFormat, va_list va)
{
    RTStrmPrintf(g_pStdErr, "Error: ");
    RTStrmPrintfV(g_pStdErr, pszFormat, va);
    Log(("autostartSvcDisplayErrorV: %s", pszFormat)); /** @todo format it! */
    return RTEXITCODE_FAILURE;
}

DECLHIDDEN(RTEXITCODE) autostartSvcDisplayError(const char *pszFormat, ...)
{
    va_list va;
    va_start(va, pszFormat);
    autostartSvcDisplayErrorV(pszFormat, va);
    va_end(va);
    return RTEXITCODE_FAILURE;
}

DECLHIDDEN(RTEXITCODE) autostartSvcDisplayGetOptError(const char *pszAction, int rc, PCRTGETOPTUNION pValue)
{
    char szMsg[4096];
    RTGetOptFormatError(szMsg, sizeof(szMsg), rc, pValue);
    autostartSvcDisplayError("%s - %s", pszAction, szMsg);
    return RTEXITCODE_SYNTAX;
}

DECLHIDDEN(int) autostartSetup(void)
{
    autostartSvcOsLogStr("Setting up ...\n", AUTOSTARTLOGTYPE_VERBOSE);

    /*
     * Initialize COM.
     */
    using namespace com;
    HRESULT hrc = com::Initialize();
# ifdef VBOX_WITH_XPCOM
    if (hrc == NS_ERROR_FILE_ACCESS_DENIED)
    {
        char szHome[RTPATH_MAX] = "";
        com::GetVBoxUserHomeDirectory(szHome, sizeof(szHome));
        autostartSvcLogError("Failed to initialize COM because the global settings directory '%s' is not accessible!", szHome);
        return VERR_COM_FILE_ERROR;
    }
# endif
    if (FAILED(hrc))
    {
        autostartSvcLogError("Failed to initialize COM (%Rhrc)!", hrc);
        return VERR_COM_UNEXPECTED;
    }

    hrc = g_pVirtualBoxClient.createInprocObject(CLSID_VirtualBoxClient);
    if (FAILED(hrc))
    {
        RTMsgError("Failed to create the VirtualBoxClient object (%Rhrc)!", hrc);
        com::ErrorInfo info;
        if (!info.isFullAvailable() && !info.isBasicAvailable())
        {
            com::GluePrintRCMessage(hrc);
            autostartSvcLogError("Most likely, the VirtualBox COM server is not running or failed to start.");
        }
        else
            com::GluePrintErrorInfo(info);
        return VERR_COM_UNEXPECTED;
    }

    /*
     * Setup VirtualBox + session interfaces.
     */
    hrc = g_pVirtualBoxClient->COMGETTER(VirtualBox)(g_pVirtualBox.asOutParam());
    if (SUCCEEDED(hrc))
    {
        hrc = g_pSession.createInprocObject(CLSID_Session);
        if (FAILED(hrc))
            autostartSvcLogError("Failed to create a session object (rc=%Rhrc)!", hrc);
    }
    else
        autostartSvcLogError("Failed to get VirtualBox object (rc=%Rhrc)!", hrc);

    if (FAILED(hrc))
        return VERR_COM_OBJECT_NOT_FOUND;

    return VINF_SUCCESS;
}

DECLHIDDEN(void) autostartShutdown(void)
{
    autostartSvcOsLogStr("Shutting down ...\n", AUTOSTARTLOGTYPE_VERBOSE);

    g_pSession.setNull();
    g_pVirtualBox.setNull();
    g_pVirtualBoxClient.setNull();
    com::Shutdown();
}