VirtualBox

source: vbox/trunk/src/VBox/Main/src-client/ClientTokenHolder.cpp@ 96407

Last change on this file since 96407 was 96407, checked in by vboxsync, 2 years ago

scm copyright and license note update

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Revision
File size: 10.1 KB
Line 
1/* $Id: ClientTokenHolder.cpp 96407 2022-08-22 17:43:14Z vboxsync $ */
2/** @file
3 *
4 * VirtualBox API client session token holder (in the client process)
5 */
6
7/*
8 * Copyright (C) 2006-2022 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#define LOG_GROUP LOG_GROUP_MAIN_SESSION
30#include "LoggingNew.h"
31
32#include <iprt/asm.h>
33#include <iprt/assert.h>
34#include <iprt/log.h>
35#include <iprt/semaphore.h>
36#include <iprt/process.h>
37
38#ifdef VBOX_WITH_SYS_V_IPC_SESSION_WATCHER
39# include <errno.h>
40# include <sys/types.h>
41# include <sys/stat.h>
42# include <sys/ipc.h>
43# include <sys/sem.h>
44#endif
45
46#include <VBox/com/defs.h>
47
48#include "ClientTokenHolder.h"
49#include "SessionImpl.h"
50
51
52#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
53/** client token holder thread */
54static DECLCALLBACK(int) ClientTokenHolderThread(RTTHREAD hThreadSelf, void *pvUser);
55#endif
56
57
58Session::ClientTokenHolder::ClientTokenHolder()
59{
60 AssertReleaseFailed();
61}
62
63Session::ClientTokenHolder::~ClientTokenHolder()
64{
65 /* release the client token */
66#if defined(RT_OS_WINDOWS)
67
68 if (mSem && mThreadSem)
69 {
70 /*
71 * tell the thread holding the token to release it;
72 * it will close mSem handle
73 */
74 ::SetEvent(mSem);
75 /* wait for the thread to finish */
76 ::WaitForSingleObject(mThreadSem, INFINITE);
77 ::CloseHandle(mThreadSem);
78
79 mThreadSem = NULL;
80 mSem = NULL;
81 mThread = NIL_RTTHREAD;
82 }
83
84#elif defined(RT_OS_OS2)
85
86 if (mThread != NIL_RTTHREAD)
87 {
88 Assert(mSem != NIL_RTSEMEVENT);
89
90 /* tell the thread holding the token to release it */
91 int vrc = RTSemEventSignal(mSem);
92 AssertRC(vrc == NO_ERROR);
93
94 /* wait for the thread to finish */
95 vrc = RTThreadUserWait(mThread, RT_INDEFINITE_WAIT);
96 Assert(RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED);
97
98 mThread = NIL_RTTHREAD;
99 }
100
101 if (mSem != NIL_RTSEMEVENT)
102 {
103 RTSemEventDestroy(mSem);
104 mSem = NIL_RTSEMEVENT;
105 }
106
107#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
108
109 if (mSem >= 0)
110 {
111 ::sembuf sop = { 0, 1, SEM_UNDO };
112 ::semop(mSem, &sop, 1);
113
114 mSem = -1;
115 }
116
117#elif defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
118
119 if (!mToken.isNull())
120 {
121 mToken->Abandon();
122 mToken.setNull();
123 }
124
125#else
126# error "Port me!"
127#endif
128}
129
130#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
131Session::ClientTokenHolder::ClientTokenHolder(const Utf8Str &strTokenId) :
132 mClientTokenId(strTokenId)
133#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
134Session::ClientTokenHolder::ClientTokenHolder(IToken *aToken) :
135 mToken(aToken)
136#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
137{
138#ifdef CTHSEMTYPE
139 mSem = CTHSEMARG;
140#endif
141#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
142 mThread = NIL_RTTHREAD;
143#endif
144
145#if defined(RT_OS_WINDOWS)
146 mThreadSem = CTHTHREADSEMARG;
147
148 /*
149 * Since there is no guarantee that the constructor and destructor will be
150 * called in the same thread, we need a separate thread to hold the token.
151 */
152
153 mThreadSem = ::CreateEvent(NULL, FALSE, FALSE, NULL);
154 AssertMsgReturnVoid(mThreadSem,
155 ("Cannot create an event sem, err=%d", ::GetLastError()));
156
157 void *data[3];
158 data[0] = (void*)strTokenId.c_str();
159 data[1] = (void*)mThreadSem;
160 data[2] = 0; /* will get an output from the thread */
161
162 /* create a thread to hold the token until signalled to release it */
163 int vrc = RTThreadCreate(&mThread, ClientTokenHolderThread, (void*)data, 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
164 AssertRCReturnVoid(vrc);
165
166 /* wait until thread init is completed */
167 DWORD wrc = ::WaitForSingleObject(mThreadSem, INFINITE);
168 AssertMsg(wrc == WAIT_OBJECT_0, ("Wait failed, err=%d\n", ::GetLastError()));
169 Assert(data[2]);
170
171 if (wrc == WAIT_OBJECT_0 && data[2])
172 {
173 /* memorize the event sem we should signal in close() */
174 mSem = (HANDLE)data[2];
175 }
176 else
177 {
178 ::CloseHandle(mThreadSem);
179 mThreadSem = NULL;
180 }
181#elif defined(RT_OS_OS2)
182 /*
183 * Since there is no guarantee that the constructor and destructor will be
184 * called in the same thread, we need a separate thread to hold the token.
185 */
186
187 int vrc = RTSemEventCreate(&mSem);
188 AssertRCReturnVoid(vrc);
189
190 void *data[3];
191 data[0] = (void*)strTokenId.c_str();
192 data[1] = (void*)mSem;
193 data[2] = (void*)false; /* will get the thread result here */
194
195 /* create a thread to hold the token until signalled to release it */
196 vrc = RTThreadCreate(&mThread, ClientTokenHolderThread, (void *) data,
197 0, RTTHREADTYPE_MAIN_WORKER, 0, "IPCHolder");
198 AssertRCReturnVoid(vrc);
199 /* wait until thread init is completed */
200 vrc = RTThreadUserWait(mThread, RT_INDEFINITE_WAIT);
201 AssertReturnVoid(RT_SUCCESS(vrc) || vrc == VERR_INTERRUPTED);
202
203 /* the thread must succeed */
204 AssertReturnVoid((bool)data[2]);
205
206#elif defined(VBOX_WITH_SYS_V_IPC_SESSION_WATCHER)
207
208# ifdef VBOX_WITH_NEW_SYS_V_KEYGEN
209 key_t key = RTStrToUInt32(strTokenId.c_str());
210 AssertMsgReturnVoid(key != 0,
211 ("Key value of 0 is not valid for client token"));
212# else /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
213 char *pszSemName = NULL;
214 RTStrUtf8ToCurrentCP(&pszSemName, strTokenId);
215 key_t key = ::ftok(pszSemName, 'V');
216 RTStrFree(pszSemName);
217# endif /* !VBOX_WITH_NEW_SYS_V_KEYGEN */
218 int s = ::semget(key, 0, 0);
219 AssertMsgReturnVoid(s >= 0,
220 ("Cannot open semaphore, errno=%d", errno));
221
222 /* grab the semaphore */
223 ::sembuf sop = { 0, -1, SEM_UNDO };
224 int rv = ::semop(s, &sop, 1);
225 AssertMsgReturnVoid(rv == 0,
226 ("Cannot grab semaphore, errno=%d", errno));
227 mSem = s;
228
229#elif defined(VBOX_WITH_GENERIC_SESSION_WATCHER)
230
231 /* nothing to do */
232
233#else
234# error "Port me!"
235#endif
236}
237
238bool Session::ClientTokenHolder::isReady()
239{
240#ifndef VBOX_WITH_GENERIC_SESSION_WATCHER
241 return mSem != CTHSEMARG;
242#else /* VBOX_WITH_GENERIC_SESSION_WATCHER */
243 return !mToken.isNull();
244#endif /* VBOX_WITH_GENERIC_SESSION_WATCHER */
245}
246
247#if defined(RT_OS_WINDOWS) || defined(RT_OS_OS2)
248/** client token holder thread */
249DECLCALLBACK(int) ClientTokenHolderThread(RTTHREAD hThreadSelf, void *pvUser)
250{
251 RT_NOREF(hThreadSelf);
252 LogFlowFuncEnter();
253
254 Assert(pvUser);
255
256 void **data = (void **)pvUser;
257
258# if defined(RT_OS_WINDOWS)
259 Utf8Str strSessionId = (const char *)data[0];
260 HANDLE initDoneSem = (HANDLE)data[1];
261
262 Bstr bstrSessionId(strSessionId);
263 HANDLE mutex = ::OpenMutex(MUTEX_ALL_ACCESS, FALSE, bstrSessionId.raw());
264
265 //AssertMsg(mutex, ("cannot open token, err=%u\n", ::GetLastError()));
266 AssertLogRelMsg(mutex, ("cannot open token %ls, err=%u\n", bstrSessionId.raw(), ::GetLastError()));
267 if (mutex)
268 {
269 /* grab the token */
270 DWORD wrc = ::WaitForSingleObject(mutex, 0);
271 AssertMsg(wrc == WAIT_OBJECT_0, ("cannot grab token, err=%d\n", wrc));
272 if (wrc == WAIT_OBJECT_0)
273 {
274 HANDLE finishSem = ::CreateEvent(NULL, FALSE, FALSE, NULL);
275 AssertMsg(finishSem, ("cannot create event sem, err=%d\n", ::GetLastError()));
276 if (finishSem)
277 {
278 data[2] = (void*)finishSem;
279 /* signal we're done with init */
280 ::SetEvent(initDoneSem);
281 /* wait until we're signaled to release the token */
282 ::WaitForSingleObject(finishSem, INFINITE);
283 /* release the token */
284 LogFlow(("ClientTokenHolderThread(): releasing token...\n"));
285 BOOL fRc = ::ReleaseMutex(mutex);
286 AssertMsg(fRc, ("cannot release token, err=%d\n", ::GetLastError())); NOREF(fRc);
287 ::CloseHandle(mutex);
288 ::CloseHandle(finishSem);
289 }
290 }
291 }
292
293 /* signal we're done */
294 ::SetEvent(initDoneSem);
295# elif defined(RT_OS_OS2)
296 Utf8Str strSessionId = (const char *)data[0];
297 RTSEMEVENT finishSem = (RTSEMEVENT)data[1];
298
299 LogFlowFunc(("strSessionId='%s', finishSem=%p\n", strSessionId.c_str(), finishSem));
300
301 HMTX mutex = NULLHANDLE;
302 APIRET arc = ::DosOpenMutexSem((PSZ)strSessionId.c_str(), &mutex);
303 AssertMsg(arc == NO_ERROR, ("cannot open token, arc=%ld\n", arc));
304
305 if (arc == NO_ERROR)
306 {
307 /* grab the token */
308 LogFlowFunc(("grabbing token...\n"));
309 arc = ::DosRequestMutexSem(mutex, SEM_IMMEDIATE_RETURN);
310 AssertMsg(arc == NO_ERROR, ("cannot grab token, arc=%ld\n", arc));
311 if (arc == NO_ERROR)
312 {
313 /* store the answer */
314 data[2] = (void*)true;
315 /* signal we're done */
316 int vrc = RTThreadUserSignal(Thread);
317 AssertRC(vrc);
318
319 /* wait until we're signaled to release the token */
320 LogFlowFunc(("waiting for termination signal..\n"));
321 vrc = RTSemEventWait(finishSem, RT_INDEFINITE_WAIT);
322 Assert(arc == ERROR_INTERRUPT || ERROR_TIMEOUT);
323
324 /* release the token */
325 LogFlowFunc(("releasing token...\n"));
326 arc = ::DosReleaseMutexSem(mutex);
327 AssertMsg(arc == NO_ERROR, ("cannot release token, arc=%ld\n", arc));
328 }
329 ::DosCloseMutexSem(mutex);
330 }
331
332 /* store the answer */
333 data[1] = (void*)false;
334 /* signal we're done */
335 int vrc = RTThreadUserSignal(Thread);
336 AssertRC(vrc);
337# else
338# error "Port me!"
339# endif
340
341 LogFlowFuncLeave();
342
343 return 0;
344}
345#endif
346
347/* 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