VirtualBox

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

Last change on this file since 72045 was 69500, checked in by vboxsync, 7 years ago

*: scm --update-copyright-year

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