VirtualBox

source: vbox/trunk/src/VBox/Main/src-server/TokenImpl.cpp@ 98351

Last change on this file since 98351 was 98292, checked in by vboxsync, 23 months ago

Main/src-server: rc -> hrc/vrc. Enabled scm rc checks. bugref:10223

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 5.9 KB
Line 
1/* $Id: TokenImpl.cpp 98292 2023-01-25 01:14:53Z vboxsync $ */
2/** @file
3 * Token COM class implementation - MachineToken and MediumLockToken
4 */
5
6/*
7 * Copyright (C) 2013-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#define LOG_GROUP LOG_GROUP_MAIN_TOKEN
29#include "TokenImpl.h"
30#include "MachineImpl.h"
31#include "MediumImpl.h"
32#include "AutoCaller.h"
33#include "LoggingNew.h"
34
35// constructor / destructor
36/////////////////////////////////////////////////////////////////////////////
37
38DEFINE_EMPTY_CTOR_DTOR(MachineToken)
39
40HRESULT MachineToken::FinalConstruct()
41{
42 return BaseFinalConstruct();
43}
44
45void MachineToken::FinalRelease()
46{
47 uninit(false);
48
49 BaseFinalRelease();
50}
51
52// public initializer/uninitializer for internal purposes only
53/////////////////////////////////////////////////////////////////////////////
54
55/**
56 * Initializes the token object.
57 *
58 * @param pSessionMachine Pointer to a SessionMachine object.
59 */
60HRESULT MachineToken::init(const ComObjPtr<SessionMachine> &pSessionMachine)
61{
62 LogFlowThisFunc(("pSessionMachine=%p\n", &pSessionMachine));
63
64 ComAssertRet(!pSessionMachine.isNull(), E_INVALIDARG);
65
66 /* Enclose the state transition NotReady->InInit->Ready */
67 AutoInitSpan autoInitSpan(this);
68 AssertReturn(autoInitSpan.isOk(), E_FAIL);
69
70 m.pSessionMachine = pSessionMachine;
71
72 /* Confirm a successful initialization */
73 autoInitSpan.setSucceeded();
74
75 return S_OK;
76}
77
78/**
79 * Uninitializes the instance and sets the ready flag to FALSE.
80 * Called either from FinalRelease() or by the parent when it gets destroyed.
81 */
82void MachineToken::uninit(bool fAbandon)
83{
84 LogFlowThisFunc(("\n"));
85
86 /* Enclose the state transition Ready->InUninit->NotReady */
87 AutoUninitSpan autoUninitSpan(this);
88 if (autoUninitSpan.uninitDone())
89 return;
90
91 /* Destroy the SessionMachine object, check is paranoia */
92 if (!m.pSessionMachine.isNull())
93 {
94 m.pSessionMachine->uninit(fAbandon ? SessionMachine::Uninit::Normal : SessionMachine::Uninit::Abnormal);
95 m.pSessionMachine.setNull();
96 }
97}
98
99// IToken methods
100/////////////////////////////////////////////////////////////////////////////
101
102HRESULT MachineToken::abandon(AutoCaller &aAutoCaller)
103{
104 /* have to release the AutoCaller before calling uninit(), self-deadlock */
105 aAutoCaller.release();
106
107 /* uninit does everything we need */
108 uninit(true);
109 return S_OK;
110}
111
112HRESULT MachineToken::dummy()
113{
114 /* Remember, the wrapper contains the AutoCaller, which means that after
115 * uninit() this code won't be reached any more. */
116
117 /* this is a NOOP, no need to lock */
118
119 return S_OK;
120}
121
122// public methods only for internal purposes
123/////////////////////////////////////////////////////////////////////////////
124
125
126// constructor / destructor
127/////////////////////////////////////////////////////////////////////////////
128
129DEFINE_EMPTY_CTOR_DTOR(MediumLockToken)
130
131HRESULT MediumLockToken::FinalConstruct()
132{
133 return BaseFinalConstruct();
134}
135
136void MediumLockToken::FinalRelease()
137{
138 uninit();
139
140 BaseFinalRelease();
141}
142
143// public initializer/uninitializer for internal purposes only
144/////////////////////////////////////////////////////////////////////////////
145
146/**
147 * Initializes the token object.
148 *
149 * @param pMedium Pointer to a Medium object.
150 * @param fWrite True if this is a write lock, false otherwise.
151 */
152HRESULT MediumLockToken::init(const ComObjPtr<Medium> &pMedium, bool fWrite)
153{
154 LogFlowThisFunc(("pMedium=%p\n", &pMedium));
155
156 ComAssertRet(!pMedium.isNull(), E_INVALIDARG);
157
158 /* Enclose the state transition NotReady->InInit->Ready */
159 AutoInitSpan autoInitSpan(this);
160 AssertReturn(autoInitSpan.isOk(), E_FAIL);
161
162 m.pMedium = pMedium;
163 m.fWrite = fWrite;
164
165 /* Confirm a successful initialization */
166 autoInitSpan.setSucceeded();
167
168 return S_OK;
169}
170
171/**
172 * Uninitializes the instance and sets the ready flag to FALSE.
173 * Called either from FinalRelease() or by the parent when it gets destroyed.
174 */
175void MediumLockToken::uninit()
176{
177 LogFlowThisFunc(("\n"));
178
179 /* Enclose the state transition Ready->InUninit->NotReady */
180 AutoUninitSpan autoUninitSpan(this);
181 if (autoUninitSpan.uninitDone())
182 return;
183
184 /* Release the appropriate lock, check is paranoia */
185 if (!m.pMedium.isNull())
186 {
187 if (m.fWrite)
188 {
189 HRESULT hrc = m.pMedium->i_unlockWrite(NULL);
190 AssertComRC(hrc);
191 }
192 else
193 {
194 HRESULT hrc = m.pMedium->i_unlockRead(NULL);
195 AssertComRC(hrc);
196 }
197 m.pMedium.setNull();
198 }
199}
200
201// IToken methods
202/////////////////////////////////////////////////////////////////////////////
203
204HRESULT MediumLockToken::abandon(AutoCaller &aAutoCaller)
205{
206 /* have to release the AutoCaller before calling uninit(), self-deadlock */
207 aAutoCaller.release();
208
209 /* uninit does everything we need */
210 uninit();
211 return S_OK;
212}
213
214HRESULT MediumLockToken::dummy()
215{
216 /* Remember, the wrapper contains the AutoCaller, which means that after
217 * uninit() this code won't be reached any more. */
218
219 /* this is a NOOP, no need to lock */
220
221 return S_OK;
222}
223
224// public methods only for internal purposes
225/////////////////////////////////////////////////////////////////////////////
226
227/* vi: set tabstop=4 shiftwidth=4 expandtab: */
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