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