VirtualBox

source: vbox/trunk/src/VBox/Frontends/VBoxAutostart/VBoxAutostartStart.cpp@ 102493

Last change on this file since 102493 was 98103, checked in by vboxsync, 23 months ago

Copyright year updates by scm.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 7.8 KB
Line 
1/* $Id: VBoxAutostartStart.cpp 98103 2023-01-17 14:15:46Z vboxsync $ */
2/** @file
3 * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
4 */
5
6/*
7 * Copyright (C) 2012-2023 Oracle and/or its affiliates.
8 *
9 * This file is part of VirtualBox base platform packages, as
10 * available from https://www.virtualbox.org.
11 *
12 * This program is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU General Public License
14 * as published by the Free Software Foundation, in version 3 of the
15 * License.
16 *
17 * This program is distributed in the hope that it will be useful, but
18 * WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * General Public License for more details.
21 *
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, see <https://www.gnu.org/licenses>.
24 *
25 * SPDX-License-Identifier: GPL-3.0-only
26 */
27
28#include <VBox/com/com.h>
29#include <VBox/com/string.h>
30#include <VBox/com/Guid.h>
31#include <VBox/com/array.h>
32#include <VBox/com/ErrorInfo.h>
33#include <VBox/com/errorprint.h>
34
35#include <iprt/errcore.h>
36#include <iprt/log.h>
37#include <iprt/message.h>
38#include <iprt/stream.h>
39#include <iprt/thread.h>
40
41#include <algorithm>
42#include <list>
43
44#include "VBoxAutostart.h"
45
46extern unsigned g_cVerbosity;
47
48using namespace com;
49
50/**
51 * VM list entry.
52 */
53typedef struct AUTOSTARTVM
54{
55 /** ID of the VM to start. */
56 Bstr strId;
57 /** Startup delay of the VM. */
58 ULONG uStartupDelay;
59} AUTOSTARTVM;
60
61static DECLCALLBACK(bool) autostartVMCmp(const AUTOSTARTVM &vm1, const AUTOSTARTVM &vm2)
62{
63 return vm1.uStartupDelay <= vm2.uStartupDelay;
64}
65
66DECLHIDDEN(int) autostartStartMain(PCFGAST pCfgAst)
67{
68 int vrc = VINF_SUCCESS;
69 std::list<AUTOSTARTVM> listVM;
70 uint32_t uStartupDelay = 0;
71
72 autostartSvcLogVerbose(1, "Starting machines ...\n");
73
74 pCfgAst = autostartConfigAstGetByName(pCfgAst, "startup_delay");
75 if (pCfgAst)
76 {
77 if (pCfgAst->enmType == CFGASTNODETYPE_KEYVALUE)
78 {
79 vrc = RTStrToUInt32Full(pCfgAst->u.KeyValue.aszValue, 10, &uStartupDelay);
80 if (RT_FAILURE(vrc))
81 return autostartSvcLogErrorRc(vrc, "'startup_delay' must be an unsigned number");
82 }
83 }
84
85 if (uStartupDelay)
86 {
87 autostartSvcLogVerbose(1, "Delaying start for %RU32 seconds ...\n", uStartupDelay);
88 vrc = RTThreadSleep(uStartupDelay * 1000);
89 }
90
91 if (vrc == VERR_INTERRUPTED)
92 return VINF_SUCCESS;
93
94 /*
95 * Build a list of all VMs we need to autostart first, apply the overrides
96 * from the configuration and start the VMs afterwards.
97 */
98 com::SafeIfaceArray<IMachine> machines;
99 HRESULT hrc = g_pVirtualBox->COMGETTER(Machines)(ComSafeArrayAsOutParam(machines));
100 if (SUCCEEDED(hrc))
101 {
102 /*
103 * Iterate through the collection
104 */
105 for (size_t i = 0; i < machines.size(); ++i)
106 {
107 if (machines[i])
108 {
109 Bstr strName;
110 CHECK_ERROR_BREAK(machines[i], COMGETTER(Name)(strName.asOutParam()));
111
112 BOOL fAccessible;
113 CHECK_ERROR_BREAK(machines[i], COMGETTER(Accessible)(&fAccessible));
114 if (!fAccessible)
115 {
116 autostartSvcLogVerbose(1, "Machine '%ls' is not accessible, skipping\n", strName.raw());
117 continue;
118 }
119
120 AUTOSTARTVM autostartVM;
121
122 BOOL fAutostart;
123 CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostartEnabled)(&fAutostart));
124 if (fAutostart)
125 {
126 CHECK_ERROR_BREAK(machines[i], COMGETTER(Id)(autostartVM.strId.asOutParam()));
127 CHECK_ERROR_BREAK(machines[i], COMGETTER(AutostartDelay)(&autostartVM.uStartupDelay));
128
129 listVM.push_back(autostartVM);
130 }
131
132 autostartSvcLogVerbose(1, "Machine '%ls': Autostart is %s (startup delay is %RU32 seconds)\n",
133 strName.raw(), fAutostart ? "enabled" : "disabled",
134 fAutostart ? autostartVM.uStartupDelay : 0);
135 }
136 }
137
138 /**
139 * @todo r=uwe I'm not reindenting this whole burnt offering
140 * to mistinterpreted Dijkstra's "single exit" commandment
141 * just to add this log, hence a bit of duplicate logic here.
142 */
143 if (SUCCEEDED(hrc))
144 {
145 if (machines.size() == 0)
146 autostartSvcLogWarning("No virtual machines found.\n"
147 "This either could be a configuration problem (access rights), "
148 "or there are no VMs configured yet.");
149 else if (listVM.empty())
150 autostartSvcLogWarning("No virtual machines configured for autostart.\n"
151 "Please consult the manual about how to enable auto starting VMs.\n");
152 }
153 else
154 autostartSvcLogError("Enumerating virtual machines failed with %Rhrc\n", hrc);
155
156 if ( SUCCEEDED(hrc)
157 && !listVM.empty())
158 {
159 ULONG uDelayCur = 0;
160
161 /* Sort by startup delay and apply base override. */
162 listVM.sort(autostartVMCmp);
163
164 std::list<AUTOSTARTVM>::iterator it;
165 for (it = listVM.begin(); it != listVM.end(); ++it)
166 {
167 ComPtr<IMachine> machine;
168 ComPtr<IProgress> progress;
169
170 CHECK_ERROR_BREAK(g_pVirtualBox, FindMachine((*it).strId.raw(), machine.asOutParam()));
171
172 Bstr strName;
173 CHECK_ERROR_BREAK(machine, COMGETTER(Name)(strName.asOutParam()));
174
175 if ((*it).uStartupDelay > uDelayCur)
176 {
177 autostartSvcLogVerbose(1, "Waiting for %ul seconds before starting machine '%s' ...\n",
178 (*it).uStartupDelay - uDelayCur, strName.raw());
179 RTThreadSleep(((*it).uStartupDelay - uDelayCur) * 1000);
180 uDelayCur = (*it).uStartupDelay;
181 }
182
183 CHECK_ERROR_BREAK(machine, LaunchVMProcess(g_pSession, Bstr("headless").raw(),
184 ComSafeArrayNullInParam(), progress.asOutParam()));
185 if (SUCCEEDED(hrc) && !progress.isNull())
186 {
187 autostartSvcLogVerbose(1, "Waiting for machine '%ls' to power on ...\n", strName.raw());
188 CHECK_ERROR(progress, WaitForCompletion(-1));
189 if (SUCCEEDED(hrc))
190 {
191 BOOL completed = true;
192 CHECK_ERROR(progress, COMGETTER(Completed)(&completed));
193 if (SUCCEEDED(hrc))
194 {
195 ASSERT(completed);
196
197 LONG iRc;
198 CHECK_ERROR(progress, COMGETTER(ResultCode)(&iRc));
199 if (SUCCEEDED(hrc))
200 {
201 if (FAILED(iRc))
202 {
203 ProgressErrorInfo info(progress);
204 com::GluePrintErrorInfo(info);
205 }
206 else
207 autostartSvcLogVerbose(1, "Machine '%ls' has been successfully started.\n", strName.raw());
208 }
209 }
210 }
211 }
212 SessionState_T enmSessionState;
213 CHECK_ERROR(g_pSession, COMGETTER(State)(&enmSessionState));
214 if (SUCCEEDED(hrc) && enmSessionState == SessionState_Locked)
215 g_pSession->UnlockMachine();
216 }
217 }
218 }
219
220 return vrc;
221}
222
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