VirtualBox

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

Last change on this file since 106814 was 106061, checked in by vboxsync, 3 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.9 KB
Line 
1/* $Id: VBoxAutostartStart.cpp 106061 2024-09-16 14:03:52Z vboxsync $ */
2/** @file
3 * VBoxAutostart - VirtualBox Autostart service, start machines during system boot.
4 */
5
6/*
7 * Copyright (C) 2012-2024 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 ComPtr<IMachine> pMachine = machines[i];
108
109 if (pMachine.isNotNull())
110 {
111 Bstr strName;
112 CHECK_ERROR_BREAK(pMachine, COMGETTER(Name)(strName.asOutParam()));
113
114 BOOL fAccessible;
115 CHECK_ERROR_BREAK(pMachine, COMGETTER(Accessible)(&fAccessible));
116 if (!fAccessible)
117 {
118 autostartSvcLogVerbose(1, "Machine '%ls' is not accessible, skipping\n", strName.raw());
119 continue;
120 }
121
122 AUTOSTARTVM autostartVM;
123
124 BOOL fAutostart;
125 CHECK_ERROR_BREAK(pMachine, COMGETTER(AutostartEnabled)(&fAutostart));
126 if (fAutostart)
127 {
128 CHECK_ERROR_BREAK(pMachine, COMGETTER(Id)(autostartVM.strId.asOutParam()));
129 CHECK_ERROR_BREAK(pMachine, COMGETTER(AutostartDelay)(&autostartVM.uStartupDelay));
130
131 listVM.push_back(autostartVM);
132 }
133
134 autostartSvcLogVerbose(1, "Machine '%ls': Autostart is %s (startup delay is %RU32 seconds)\n",
135 strName.raw(), fAutostart ? "enabled" : "disabled",
136 fAutostart ? autostartVM.uStartupDelay : 0);
137 }
138 }
139
140 /**
141 * @todo r=uwe I'm not reindenting this whole burnt offering
142 * to mistinterpreted Dijkstra's "single exit" commandment
143 * just to add this log, hence a bit of duplicate logic here.
144 */
145 if (SUCCEEDED(hrc))
146 {
147 if (machines.size() == 0)
148 autostartSvcLogWarning("No virtual machines found.\n"
149 "This either could be a configuration problem (access rights), "
150 "or there are no VMs configured yet.");
151 else if (listVM.empty())
152 autostartSvcLogWarning("No virtual machines configured for autostart.\n"
153 "Please consult the manual about how to enable auto starting VMs.\n");
154 }
155 else
156 autostartSvcLogError("Enumerating virtual machines failed with %Rhrc\n", hrc);
157
158 if ( SUCCEEDED(hrc)
159 && !listVM.empty())
160 {
161 ULONG uDelayCur = 0;
162
163 /* Sort by startup delay and apply base override. */
164 listVM.sort(autostartVMCmp);
165
166 std::list<AUTOSTARTVM>::iterator it;
167 for (it = listVM.begin(); it != listVM.end(); ++it)
168 {
169 ComPtr<IMachine> machine;
170 ComPtr<IProgress> progress;
171
172 CHECK_ERROR_BREAK(g_pVirtualBox, FindMachine((*it).strId.raw(), machine.asOutParam()));
173
174 Bstr strName;
175 CHECK_ERROR_BREAK(machine, COMGETTER(Name)(strName.asOutParam()));
176
177 if ((*it).uStartupDelay > uDelayCur)
178 {
179 autostartSvcLogVerbose(1, "Waiting for %ul seconds before starting machine '%s' ...\n",
180 (*it).uStartupDelay - uDelayCur, strName.raw());
181 RTThreadSleep(((*it).uStartupDelay - uDelayCur) * 1000);
182 uDelayCur = (*it).uStartupDelay;
183 }
184
185 CHECK_ERROR_BREAK(machine, LaunchVMProcess(g_pSession, Bstr("headless").raw(),
186 ComSafeArrayNullInParam(), progress.asOutParam()));
187 if (SUCCEEDED(hrc) && !progress.isNull())
188 {
189 autostartSvcLogVerbose(1, "Waiting for machine '%ls' to power on ...\n", strName.raw());
190 CHECK_ERROR(progress, WaitForCompletion(-1));
191 if (SUCCEEDED(hrc))
192 {
193 BOOL completed = true;
194 CHECK_ERROR(progress, COMGETTER(Completed)(&completed));
195 if (SUCCEEDED(hrc))
196 {
197 ASSERT(completed);
198
199 LONG iRc;
200 CHECK_ERROR(progress, COMGETTER(ResultCode)(&iRc));
201 if (SUCCEEDED(hrc))
202 {
203 if (FAILED(iRc))
204 {
205 ProgressErrorInfo info(progress);
206 com::GluePrintErrorInfo(info);
207 }
208 else
209 autostartSvcLogVerbose(1, "Machine '%ls' has been successfully started.\n", strName.raw());
210 }
211 }
212 }
213 }
214 SessionState_T enmSessionState;
215 CHECK_ERROR(g_pSession, COMGETTER(State)(&enmSessionState));
216 if (SUCCEEDED(hrc) && enmSessionState == SessionState_Locked)
217 g_pSession->UnlockMachine();
218 }
219 }
220 }
221
222 return vrc;
223}
224
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